PHP variables can store different data types. Unlike Java, .Net, C#, In PHP we do not require a keyword to define the data type. PHP engine is designed in a way that it can understand the type of data from the assigned value.
So what are the data types which PHP engine understands?
PHP Data Types
PHP supports following types of data.
- String
- Integer
- Float (decimal numbers)
- Boolean
- Array
String
We assign string values keeping in between single quote or double quotes.
Example
<?php
$fruit_name=”Orange”;
echo $fruit_name;
?>
Integer
We define integer data types by assigning integer values
Example
<?php
$age=22;
echo $age;
?>
Float
We define float datatype by assigning float values.
Example
<?php
$mobile-price=2222.40
echo $mobile-price;
?>
Array
We define array by keeping elements in between array function (We will discuss about array in detail in next chapter).
Example
<?php
$fruitList=array(“apple”,”orange”,”mango”,”guava”);
echo $fruitList[0];
?>
(If you are not aware about array at all, then ignore the above code. In next chapter we will discuss about it in detail. )
The above code will print – apple
Boolean
Boolean data types are useful to define datatypes in either true or false. We define Boolean data type by assigning true or false value.
Example
<?php
$isItColdToday=true;
$isEmployeeRetired=false;
echo $isItColdToday;
echo $isEmployeeRetired;
?>
The above code will print true, false.
Note: While defining boolean value, we do not keep true or false in between quote. Thats because boolean values true or false are reserved and we do not need quote for it. If we put true or false in between single quote or double quote then it will not be any more a boolean value, it will be a string value.
There are few more data types exist. We will discuss about it in future chapters.