Data Types in PHP
PHP has a total of eight data types which we use to construct our variables to store data of different types.
PHP supports the following data types:
- String : are sequences of characters, like ‘PHP World’. A string can be any text inside quotes. You can use single or double quotes.
Example
[php]
<?php $x = "PHP World"; echo $x; ?>
[/php] - Integer : are whole numbers, without a decimal point. An integer can be either positive or negative.
Example
[php]
<?php $x = 5985; var_dump($x); ?>
[/php] - Float or Double : are floating point numbers with a decimal point
Example
[php]
<?php $x = 10.365; var_dump($x); ?>
[/php] - Boolean : only two possible values either TRUE or FALSE
Example
[php]
<?php
$x = TRUE;
$y = FALSE;
if ($x) {
print("This is true<br>");
} else {
print("This is false<br>");
}
?>
[/php] - Array : stores multiple values in one single variable.
Example
[php]
<?php
$arr = array("PHP","MySql","HTML");
var_dump($arr);
?>
[/php] - Object : are instances of classes, which stores data and information on how to process that data
Example
[php]
<?php
class Skills {
function Skills() {
$this->lang = "JS";
}
}// create an object
$getlang = new Skills();// show object properties
echo $getlang->lang;
?>
[/php] - NULL : is a special data type that only has one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it. If a variable is created without a value, it is automatically assigned a value of NULL. Also a variables can be emptied by assigning a value of NULL.
Example
[php]
<?php
$x = "PHP World";
$x = null;
var_dump($x);
?>
[/php] - Resource : is not an actual data type. It holds references to resources external to PHP
A common example of using the resource data type is a database call.
Note : The PHP var_dump() function returns the data type and value