In this data types tutorial we are going to learn about various data types used in PHP.
But before that we will be learning about print statement which has the same function of displaying data as the echo statement.
- Print statement:
- print statement is also used to display something on the webpage or browser.
- It is similar to echo statement.
- But unlike echo statement, print statement can display only one string and returns value 1.
- Like echo statement print statement can also be used with or without parenthesis ().
- Print like echo statements can be used to display strings, values of variables etc.
- But compared to echo, print is slower in execution since it returns a value.
- To view it practically create a new folder datatypes in the htdocs folder of xampp folder and then create an index.php file in the newly created datatypes folder.
- Write the following code to demonstrate print statement in the index.php file:
- The above given code contains print statements that has string, HTML tags etc.
- We can also use concatenation operator period (.) to join two strings in print statement like echo statement.
- We can even print value of a variable using print statement just like echo statement.
- The output is shown below:
- But we had quoted first that print statement can print only one string and not multiple strings. This will be understood by the following example code:
- Write the following echo statement first and see the output:
- This echo statement has 5 strings separated with comma. All these strings are displayed in the output shown below:
- But now let us try to display multiple strings at a time using print statement. Write the following code given below:
- This print statement cannot display multiple strings separated by comma at a time.
- This statement will give an error as shown below:
- Data Types:
- Data Types in general are the types that allow storage of a particular type of value.
- A memory location occupied by a variable is commanded to allow storage of a particular type of values using data types.
- But PHP is a loosely typed language, hence there is no need to specify about which data type value a variable should hold.
- PHP has the ability to decide a data type of a particular variable according to the value stored in it.
- The data types in PHP are,
- Let us study about each data type in detail:
$friend="Friends like you are very few.";
print "<h2>A friend in need is a friend indeed!</h2>";
print "Roses are red,"." Skies are blue,<br>";
echo "This", " is", " echo", " statement", " able to print multiple strings at a time.";
print "This", " is", " print", " statement", " unable to print multiple strings at a time.";
Thus we have learned one more method to display data in the form of print statement.
- A string is a sequence of characters.
- Any words, characters, numbers, symbols or sentence enclosed in double or single quotes is termed as a string.
- Eg. “I’m learning Data Types in PHP”,”12345”.
- The variable storing such values is said to store a String data type value.
- An integer is a number without decimal point.
- Integer contains at least one number from 0 to 9.
- Integers don’t have decimal points.
- You cannot have comma or blanks in an integer number.
- Integers can be negative or positive.
- Integers can be specified in 3 formats
- Decimal (base 10)
- Eg: $x=5689;
- These numbers are prefixed with 0x.
- Eg: $y=0x80;
- These numbers are prefixed with 0.
- Eg: $z=045;
- Floating point numbers are the numbers with decimal point or the numbers in exponential format.
- These are also called as floats, doubles or real numbers.
- Eg: $a=276.4;
- Boolean variables can have either true or false values.
- They can be used mostly in conditional statements.
- Mostly the value zero (0) evaluates to false and the value other than zero evaluates to true.
- Eg: $b=true;
- Eg: $S=false;
- Array is a data structure that can store number of similar type values in a single variable.
- Eg: $fruits=array(“Apple”,”Mango”,”Orange”,”Guava”);
- It is a data type used to store objects.
- Objects are the instances of a program construct called Class.
- Class is an entity that stores data and methods to process the data.
- Objects for classes are declared explicitly.
- Eg: $bus=new bus();
- The variable containing no value contains NULL value.
- NULL is only possible value of type NULL.
- This is used to make a variable empty. i.e. NULL value is assigned to a variable to make it empty.
- The NULL value can be used to identify whether a variable is empty.
- Eg. $bag=null;
- Here, in the above code we have used variable $x which is assigned different type of value every time.
- If it is assigned a string like “Caroline”, it becomes a string. If it is assigned an integer like 123, it becomes an integer. A float value makes it a float.
- We have seen before that value 0 indicates false and value other than zero indicates true in Boolean data type. It can behave vice-versa also.
- All the values assigned to variable $x are displayed using echo statement.
- The output is shown below:
- Here, we have declared various variables with different types of values.
- These variables are passed to inbuilt var_dump() function each.
- The task of var_dump() function is to return the data type of the variable passed as a parameter to it.
- The output is shown below:
- Type Conversion:
- There are certain situations when we need to convert the variables from one data type to another.
- There are two ways to convert variables from one type to another.
- Implicit conversion
- Explicit conversion
- Implicit Conversion:
- The most common way of conversion of variable from one type to another is the implicit conversion.
- This is the conversion which the PHP language engine handles automatically.
- This conversion takes place automatically when a certain operation containing values of different data types is performed.
- Examples of most places where implicit conversions take place are:
- If one float value and one integer value is added the result of the expression will be converted to float data type.
- In the places where the expressions need to be evaluated to Boolean type, they are automatically converted to Boolean type.
- Certain methods that expect everything to be converted to string, there each value is converted to string. For eg. In echo statement.
- In some cases where PHP does not convert variables to your desired data types, there we have to force the variables to be converted to other data types by using type casting.
- Type casting in PHP is much like that used in C programming language.
- To explicitly convert a variable you just have to prefix the variable with the desired data type surrounded by parenthesis.
- Let us see which casts should we use for explicit conversions:
- For type casting to integer data type use (int) or (integer).
- For type casting to floating point data type use (float) or (double) or (real).
- For type casting to string data type use (string).
- For type casting to Boolean data type use (bool) or (boolean).
- For type casting to array data type use (array).
- For type casting to object data type use (object).
- Examples of some type casts are given below:
- Echo (int)3.99;
- Here the value after decimal point is truncated and 3 is displayed.
- The output here is integer type 123.
- The output here will be float type 126.56.
- The output here is 1, since all numbers other than 0 returns true and 1 represents true.
$f = (float)"126.56";
- There are certain useful functions used to identify data types of variables.
- The functions are as follows:
- is_type function:
- In is_type function the word type is to be replaced with the desired data type.
- The is_type function is used to identify whether a particular variable is of the specified type.
- This function returns true if the variable is of the specified type otherwise it returns false.
- Is_type is the general form of the function. This function is used as is_integer, is_null, is_string, is_float, is_bool, is_object, is_array etc.
- This gettype function is similar to is_type function. The only thing is that it doesn’t return Boolean values but it returns names of the data types directly.
- The types returned are “boolean” for Boolean, “integer” for integer, “string” for string type, “array” for array type, “object” for object type, “double” for float type (float is not returned).
- But this function is not used frequently because is_type function is mostly used.
- This settype function is used to convert a variable from one type to another.
- This settype function takes two parameters. First is the variable to be converted and second is the data type to which the variable is to be converted.
- This function returns a Boolean value that indicates whether the conversion was successful or not.
- Eg: $no=1000;
- The converted value in the string format i.e. “1000” will be stored in the $no variable itself.
- But as settype function does the same work like type casting it is not mostly used.
Thus we went through the data types in PHP and details about it in this PHP Data Types tutorial.