Data Types in PHP 5
PHP Data types specify the different types of data that are supported in PHP language. There are total 8 data types supported in PHP, which are categorized into 3 main types. They are:
- Scalar Types:
- Compound Types:
- Special Types:
Let's cover all the different types of data types available in PHP one by one.
A boolean data type can have two possible values, either True or False.
$a = true;
$b = false;
NOTE: Here the values
false are not enclosed within quotes, because these are not strings.
An Integer data type is used to store any non-decimal numeric value within the range -2,147,483,648 to 2,147,483,647.
An integer value can be negative or positive, but it cannot have a decimal.
$x = -2671;
$y = 7007;
Float data type is used to store any decimal numeric value.
A float(floating point) value can also be either negative or positive.
$a = -2671.01;
$b = 7007.70;
String data type in PHP and in general, is a sequence of characters(or anything, it can be numbers and special characters too) enclosed within quotes. You can use single or double quotes.
$str1 = "Hello";
$str2 = "What is your Roll No?";
$str3 = "4";
echo "Me: My Roll number is $str3";
What is your Roll No?
Me: My Roll number is 4
NULL data type is a special data type which means nothing. It can only have one value, and that is
If you create any variable and do not assign any value to it, it will automatically have
NULL stored in it.
Also, we can use
NULL value to empty any variable.
// holds a null value
$b = 7007.70;
// we can also assign null value
$b = null;
PHP Array, Object and Resource Data types
We will study about these data types later on as these are advanced data types and require a better understanding of PHP basics.