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:
boolean
, integer
, float
and string
.
- Compound Types:
array
and object
.
- Special Types:
resource
and NULL
.
Let's cover all the different types of data types available in PHP one by one.
PHP Boolean
A boolean data type can have two possible values, either True or False.
$a = true;
$b = false;
NOTE: Here the values true
and false
are not enclosed within quotes, because these are not strings.
PHP Integer
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;
PHP Float
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;
PHP String
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 $str1;
echo "<br/>";
echo $str2;
echo "<br/>";
echo "Me: My Roll number is $str3";
Hello
What is your Roll No?
Me: My Roll number is 4
PHP NULL
NULL data type is a special data type which means nothing. It can only have one value, and that is NULL
.
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
$a;
$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.