PHP 8 Datatypes

The value store in a variable is of some datatype. There are eight data types in PHP. In which four are scalar, two are compound and two are special datatypes.

Four scalar datatypes of PHP 8

A scalar value contians only one value at a time.

Integers

Integers are whole numbers [..., -1, 0 ,1..] both positive and negative and without a decimal point. PHP integers are range from -2,147,483,648 to +2,147,483,647. PHP Integer can be any of these formats - number, decimal (10 base), hexadecimal (16 base) and octal notation (8 base).

Example -

<?php
	$num = 255;
    $decimal_num = 0b11001;
    $octal_num = 0144;
    $hexadecimal_num = 0x64;
    
	echo $num.'<br/>';  
    echo $decimal_num.'<br/>';  
    echo $octal_num.'<br/>';
    echo $hexadecimal_num.'<br/>';
?>

Floating Point Number

Floats or doubles number represent real numbers that have a decimal point and can be written in both positive and negative sign.

Example -

<?php
    $float_num1 = 5.3432;
    $float_num2 = 24.544;

    echo $float_num1.'<br/>';
    echo $float_num2;
?>




Booleans

Boolean has only two possible values either TRUE or FALSE. Booleans are used in many condition code statements. Comparison operator always returns a boolean value. We can also check the boolean value of other data types. Like, All non zero integers and floating values are TRUE, a blank string boolean value is FALSE.

Example -

<?php
    $var = 0;
    if($var){
        echo 'The variable is not a non zero number.';     
    } else{ 
        echo 'The given variable is a zero number.'; 
    } 
?>

Output : - The given variable is a zero number.

Strings

Strings are sequence of characters and written within single quotes or double quotes.
The double quotes are preferred, if the string contains some PHP variables. On the browser, the variables are replaced from their values. There is no any size limit of the strings. We can also write special characters in the string, but proceed with backslash(\) to handle the conflict. Like - PHP variables are starting with a $ sign. So in case, if we have a requirement to print $ sign in the string, then we write the $ sign beginning with a backslash. Like- The dollar sign is \$.

Example -

<?php
    $str1 = 'Welcome to eTutorialsPoint';
    $str2 = ' The dollar sign is \$';
    echo $str1.'<br/>';
    echo $str2;
?>




Two compound datatypes of PHP

Array

An array is a collection of key/value pairs. We can store more than one item in only a single variable i.e. Array. So Array is used when there is requirement to add more items in a single variable. We can store and retrieve any other data type, including numbers, boolean values, strings, objects and even other arrays. To create an array, we are using an array() construct. Each element in an array is separated by comma.

Example -

<?php
  $arr1 = array('Book', 'Table', 'Chair'); 
  echo '<pre>';
  print_r($arr1);
?>

We can also define keys with values. As shown in the below example.

<?php
   $arr2 = array('a1'=>'Book', 'a2'=>'Table', 'a3'=>'Chair'); 
   echo '<pre>';
   print_r($arr2);
?>

Object

Object is used to instantiate a class. With the help of new keyword we can create object of a class and using that object we can call functions of that class.

Example -

<?php
   class Animal{
     function dog() {
       return 'Dogs are honest pet animal';
     }
   }
   $obj1 = new Animal();
   echo $obj1->dog();
?>

Two special datatypes of PHP

NULL

It has only one possible value, i.e. NULL. If a variable has no any value assigned, then it has a null value. It returns false when passes on ISSET operator.

Example -

<?php
    $var = NULL;
    if(!isset($var){
        echo 'Null value' ;
    }
?>

Resources

Resources are special variables holding references to external resources. The PHP official site contains lists of resource datatypes.


Practice Exercises





Read more articles


General Knowledge



Learn Popular Language