Data Types
Data Types defines the type of data a variable can store. PHP allows eight different types of data types.
- String :- A string is a sequence of characters, like “Hello world!”.
- Integer :- An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
- Float (floating point numbers – also called double) :-A float (floating point number) is a number with a decimal point or a number in exponential form.
- Boolean :- have only two possible values either true or false.
- Array :- An array stores multiple values in one single variable.
Variables in PHP
Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;
Let’s try creating a variable containing a string, and a variable containing a number:
1 2 3 4 |
<?php $txt="Hello World!"; $x=16; ?> |
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.In the example above, you see that you do not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value.In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.In PHP, the variable is declared automatically when you use it.
Rules for Variables
- A variable name must start with a letter or an underscore “_”
- A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0- 9, and _ )
- A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
- A string variable is used to store and manipulate
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
-
Arithmetic operators
Comparison Operators
-
Assignment operators
Logical Operators
Strings
Strings can be seen as a stream of characters. For example, ‘G’ is a character and ‘BIM STUDY NOTES’ is a string. We have learned about basics of string data type in PHP in PHP | Data types and Variables.
Built-in String functions
Built-in functions in PHP are some existing library functions which can be used directly in our programs making an appropriate call to them. Below are some important built-in string functions that we use in our daily and regular programs:
1. strlen() function: This function is used to find the length of a string. This function accepts the string as argument and return the length or number of characters in the string.Example:-
1 2 3 |
?php echo strlen("Hello world!"); ?> |
output:- 12
2. strpos() function: This function takes two string arguments and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE. Example:The strpos() function is used to search for character within a
Example:-
1 2 3 |
<?php echo strpos("Hello world!","world"); ?> |
output:- 6
Note :- The position of the string “world” in our string is position 6. The reason that it is 6 (and not 7), is that the first position in the string is 0, and not
3. others:
a )strrev() function: This function is used to reverse a string.
1 2 3 |
<?php echo strrev("Hello world!"); ?> |
Output
b)str_replace() function: str_replace function is used to replaces some characters with some other characters in a string. In below example we replace world with Faiz.
1 2 3 |
<?php echo str_replace("world", "Faiz", "Hello world!"); ?> |
c)trim() function: This function allows us to remove whitespaces or strings from both sides of a string. Example:
Creating Strings
There are two ways of creating strings in PHP:
Single-quote strings: This type of strings does not processes special characters inside quotes.
1 2 3 4 5 |
<?php // single-quote strings $site = 'Welcome to GeeksforGeeks'; echo $site; ?> |
Double-quote strings : Unlike single-quote strings, double-quote strings in PHP is capable of processing special characters.
1 2 3 4 5 6 |
<?php // double-quote strings echo "Welcome to BIM STUDY NOTES \n"; $site = "BIM STUDY NOTES"; echo "Welcome to $site"; ?> |
In the above program we can see that the double-quote strings is processing the special characters according the their properties. The ‘\n’ character is not printed and is considered as a new-line. Also instead of the variable name $site, “BIM STUDY NOTES” is printed. PHP treats everything inside double quotes(” “) as Strings
Note:- Strings within single quote ignores the special characters but double-quoted strings recognize the special characters and treat them differently.