PHP datatype specifies which type of data a variable can store.
There are following data types of PHP:
- String
- Integer
- Float
- Boolean
- Array
1. PHP Strings
This is sequence of characters which can be put in whether double quotes or single quotes.
Example:
1 2 3 4 5 6 | <?php $str="Being Human"; $str1="Human Being"; echo $str."<br/>; echo $str1; ?> |
Result:
1 2 | Being Human Being Human |
2 PHP Integers
Integers are whole numbers in which there would be no decimal.
It should have at least numeric digit, do not include any comma or blank space, can be positive or negative but not any decimal part and it can only be expressed in decimal, hexadecimal and octal decimal .
Example:
1 2 3 4 5 6 7 | <?php $age=25; $quantity=45; echo $age. '<br/>'; echo $quantity . '<br/>'; var_dump($age); ?> |
Result:
1 | int(23) |
3 PHP float
This can be presented in decimal format or in exponential form.
In PHP real values are considered as php double, or float or real data type.
Example:
1 2 3 4 5 6 7 | <?php $price=25.32; $measurement=67.5454; echo $price. '<br/>'; echo $measurement . '<br/>'; var_dump($price); ?> |
Result:
1 2 3 | 25.32 67.5454 float(25.32) |
4 PHP Boolean
boolean php have two optional with them one is true and another is false.
Syntax:
$a=TRUE;
$B=FALSE;
1 2 3 4 | <?php $x=true; var_dump($x); ?> |
Result
1 | bool(true) |
5 PHP Array
It can easily store multiple values in single variable.
Example: Print integer array in PHP using for loop.
1 2 3 4 5 6 7 | <?php $nums = array(1,2,3,4,5); for($i=0;$i<5;$i++){ echo 'value is '.$nums[$i].'<br/>'; } ?> |
Result:
1 2 3 4 5 | value is 1 value is 2 value is 3 value is 4 value is 5 |
Example: Print String array in PHP using foreach loop.
1 2 3 4 5 6 | <?php $names = array("Ram","Mohan","Shyam","Rita","Geeta"); foreach ($names as $name) { echo "$name <br>"; } ?> |
Output
1 2 3 4 5 | Ram Mohan Shyam Rita Geeta |