Normally, in PHP we no need to represent the data type to a variable, depending on variable value, it converts the correct data type to variable automatically.
PHP supports following Data Types:
Integer
String
Float or double
Array
Boolean
Null
Object
Integer
Description
Integer is a positive or negative number without decimal value. Integer is of only number, without comma and decimal value.Integers can be defined in three different formats:
Decimal (base-10)
Hexadecimal (base 16 - prefixed with 0x)
Octal (base 8 - prefixed with 0)
Example: The function var_dump() returns the data type and value.
[php]
<?php
$a = 258;
var_dump($a);
?>
[/php]
String
Description
String is a collection of characters.The string can be enclosed in single or double quotes.
Example:
[php]
<?php
$a = "Welcome to SPLessons!";
$b = 'Welcome to SPLessons!';
echo $a;
echo "<br>";
echo $b;
?>
[/php]
Float
Description
The numeric value, which consists of decimal value called as floating value.
Example:
[php]
<?php
$a = 25.365;
var_dump($a);
?>
[/php]
Boolean
Description
Boolean is a data type which represents true or false.
Example:
[php]
$a = true;
$b = false;
[/php]
Array
Description
Array is a collection of variables, which stores different variable values as a single array.array() keyword is used to define an array.
Example:
[php]
<?php
$operating_systems = array("Windows","Linux","Unix");
var_dump($operating_systems);
?>
[/php]
Null
Description
Null represents no value, this is a special data type which don't assign any value to the variable.Any variable without any value assigned to it tends to NULL value.
[php]
<?php
$a = "Welcome to SPLessons!!";
$a = null;
var_dump($a);
?>
[/php]
PHP Object
Description
Object data type is used to store a data. object data type, must create a class where Class is a structure of methods and it's properties.
[php]
<?php
class OS {
function OS() {
$this->model = "Linux";
}
}
// create an object
$version = new OS();
// show object properties
echo $version->model;
?>
[/php]