Generally, the datatypes are used to hold different types of data (or) values. Datatypes specifies the different types of data that are supported in PHP.

PHP supports total eight primitive datatypes :


 Note :In PHP, var_dump() function returns the data type and value. Ex: int(123).

Integer :

In PHP, Integers hold whole numbers including positive and negative numbers (... -3, -2, -1, 0, 1, 2, 3, ...). i.e., numbers without decimal point. They can be decimal (base 10), octal (0) or hexadecimal (0x) optionally preceded by + or - sign.

Example: "intdemo.php"


<?php

$var1 
521// decimal number
echo "Decimal Number is : ".$var1."<br>";
var_dump($var1);     // it prints datatype and value
echo "<br>";
 
$var2 = -675// a negative number
echo "Negative Number is : ".$var2."<br>";
var_dump($var2);
echo 
"<br>";
 
$var3 0123// octal number
echo "Octal Number is : ".$var3."<br>";
var_dump($var3);
echo 
"<br>";

$var4 0x1B// hexadecimal number
echo "Hexadecimal Number is : ".$var4."<br>";
var_dump($var4);
?>

Output :

image

Float :

A float (floating point number) is a number with a decimal point. It hold numbers containing fractional or decimal part including positive and negative numbers.

Example: "floatdemo.php"


<?php

$var1 
23.45// float
echo "Float Number is : ".$var1."<br>";
var_dump($var1);     // it prints datatype and value
echo "<br>";
 
$var2 = -67.12// a negative float
echo "Negative Float Number is : ".$var2."<br>";
var_dump($var2);
echo 
"<br>";
 
$var3 10.2e3// double 
echo "Double Number is : ".$var3."<br>";
var_dump($var3);
?>

Output :

image

String :

A string is a sequence of characters which are enclosed with single quotes or double quotes.

Example: "stringdemo.php"


<?php

$var1 
"Hello PHP"// string
echo "String is : ".$var1."<br>";
var_dump($var1);    // it prints datatype and value
echo "<br>";
 
$var2 'Study Glance'// string
echo "String is : ".$var2."<br>";
var_dump($var2);
echo 
"<br>";
?>

Output :

image

Boolean :

A boolean datatype hold two possible values either TRUE or FALSE.

Example: "blndemo.php"


<?php

$a
TRUE// boolean
echo "Status is : ".$a."<br>";
var_dump($a);    // it prints datatype and value
echo "<br>";

$bFALSE// boolean
echo "Status is : ".$b."<br>";
var_dump($b);    // it prints datatype and value
?>

Output :

image

Array :

An array datatype can hold more than one value at a time.In other words, An array is collection of elements or items.

Example: "arydemo.php"


<?php

$numbers 
= array(1,2,3,4,5);  //array
var_dump($numbers); // prints datatype and value
echo "<br>";
?>

Output :

image

Object :

In PHP, Object is an instance of user defined class that can store both values and functions.Objects are created based on user defined class via the new keyword

Example: "objdemo.php"


<?php   

     
class msg {  
          function 
display() {  
               
$str "Hello ! Welcome ";  
               echo 
$str;  
             }  
     }  
     
$obj = new msg();  
     
$obj -> display();  
     echo 
"<br>";
     
var_dump($obj);
?>

Output :

image

Null :

In PHP, Null is a special data type that has only one value: NULL, And it is case sensitive you must write in capital letters.The special type of data type NULL defined a variable with no value.

Example: "nulldemo.php"


<?php   

    $var 
NULL;  
    echo 
$var;   //it will not give any output  
?>  

Output :

image

Resource :

In PHP, A resource is a special variable, that hold a reference to an external resource such as database connection and opened files.The Resources are not exact data type in PHP

Example: "resdemo.php"


<?php

// Open a file for reading
$fp fopen("file.txt""r");
var_dump($fp);
echo 
"<br>";
echo 
$fp;
?>  

Output :

image

 Note : PHP supports eight primitive data types which are categorized into 3 main types. They are: