Datatypes are used to hold different types of values. PHP supports the following 8 primitive datatypes. These datatypes can be further categorized into three types.
- Scalar datatypes - Scalar data types will contain only a single value for variable references.
- Special datatypes - Some datatypes which has special functions.
- Compound datatypes - The compound datatypes will have a group of data in the same type.
Following is a list of PHP data types.
Datatype | Type |
string | Scalar |
boolean | Scalar |
float | Scalar |
integer | Scalar |
null | Special |
resource | Special |
array | Compound |
object | Compound |
String datatype
A string data type is used to hold a series of characters. The latest version of PHP does not set restrictions regarding the length of a string. A string can be as large as up to 2GB.
In PHP, a string can be specified within a single or double quotes. Escape sequences for special characters will not be expanded when they occur in single quoted strings.
Example
<?php
$a = "I am a string. ";
$b = 'I am also a string.';
echo $a;
echo $b;
echo "<br />";
echo 'I will appear as \n a single line.';
?>
The above program when executed will produce the following output.
I am a string. I am also a string.
I will appear as a single line.
Boolean datatype
Boolean datatype can hold only two values – TRUE or FALSE.
Example
This program when executed will display the following output.
1 bool(false)
The var_dumb function is used to display details about a variable. Here, the value of variable a is displayed as 1 because 1 denotes TRUE.
Integer datatype
In PHP, an integer datatype is used to store any whole number between -2,147,483,648 and 2,147,483,647. It can hold positive and negative values.
Example
<?php
$a = 10;
$b = -2;
echo "$a";
echo "$b";
?>
The above program will display 10 and -2 on the screen.
Float datatype
The float datatype can host any number with a decimal point. It can hold positive and negative values.
Example
<?php
$a = 10.5;
echo $a;
?>
This program will print 10.5 on the screen. You can check the data type of the variable using the var_dumb function [Eg: echo var_dumb($a)].
Null datatype
A null datatype can only hold NULL as it’s value. A variable with null datatype will not have a value. If you declare a variable without any value, the variable will have a NULL datatype.
$a = null;
Resource datatype
Unlike other data types, the resource datatype acts as a reference or an identifier to access resource data. It is not an actual datatype.
Example
The following code is used to create a file resource object reference. With the reference to this identifier, we can perform the write, append and more file manipulation operations.
$fp = fopen("index.php",'r');
Array datatype
Arrays are used to hold a group of values with the same data type. Each value of an array can have an index (numeric or associative). These values can be used to access the elements of an array.
Example
<?php
$a = array("Apple","Orange","Lemon");
var_dump($a);
?>
The above program when executed will produce the following output.
array(3) { [0]=> string(5) "Apple" [1]=> string(6) "Orange" [2]=> string(5) "Lemon" }
Object datatype
The object is a data type stores not only data but also information on how to process that data. Objects must be explicitly declared in PHP.
At first, we must declare a class of object. A class is a structure which contains properties and methods. A class can be declared using the class keyword.
Example
<?php
class Person
{
function details()
{
$this->age = 35;
}
}
$man = new Person();
echo $man->age;
?>
This program when executed will produce 35 as the output.