Data Types

In any programming language, data type describes the kind of value that can be stored in a variable and also the type of operations that can be performed on it. PHP supports ten primitive data types which are described as under :

Scalar Data Type

These are the predefined data types which can hold only a single value.

1. BOOLEAN : Boolean data type can hold only two values i.e. TRUE or FALSE. It is most commonly used with conditional statements. Example:

<?php
                                         if(TRUE){
                                              print(“Statements for TRUE”);
                                         }
                                         if(FALSE){
                                             print (“Statements for FALSE”);
                                         }
                              ?> 

2. INTEGER : Integer data type is a non-decimal number. Rules for declaring integer data type are as:

  • must have at least one digit.
  • must not contain any decimal point.
  • Integers can be specified in decimal, hexadecimal, octal or binary notation
  • It can range from -2,147,483,648 and 2,147,483,647.

Example:

<?php
                               $num = 201;
                               var_dump($num);
                         ?> 

3. FLOAT : Float data type is a decimal number or a number in an exponential form. Example:

<?php
                               $num_float = 201.986;
                               var_dump($num_float);
                         ?> 

  4. STRING : String data type is a sequence of characters. It can be represented as a text in quotes. Quotes can be single or double. Example:

<?php
                               $str = ”Hello”;
                               var_dump($str);
                    ?> 

Compound Data Type

These are the predefined data types which can hold only a single value.

1. ARRAY: An array is a sequential collection of elements stored in a single variable.

<?php
                               $days = array("Sunday", "Monday", "Tuesday");
                               var_dump($days);
                        ?>

2. OBJECT : Objects have their own states and behaviours. An object is an instance of a class whereas a class is a blueprint which describes the state and behaviour that belongs to a particular object.

If you consider a human , state will be : name, age, height, weight etc, behaviour will be : walking, laughing, sleeping etc.

On creating a __construct() function, PHP will automatically call this function when you create an object of the related class.

<?php
                               class Person{
  public $name;
  public $age;
  public function __construct($name, $age) {
	$this->name = $name;
	$this->age = $age;
  }
  public function output() {
	echo "Name  ->  " . $this->name ;
            echo "<br>Age ->  " . $this->age ;
  }
                               }

                                    $obj = new Person("Smith", 24);
$obj -> output();
echo "<br>";
                                    $obj = new Person("John", 30);
$obj -> output();
                        ?> 

3. CALLABLE : Callable keyword is used to pass a function argument as a reference to a function.

A callable can be any one of the following :

  1. An anonymous method.
  2. A string having the name of the method.
  3. An array describing an object method or a static class method.
<?php
                               function callable_method(callable $format_type, $str) {
  echo $format_type($str)." <br>";
}

class demoClass{
  public static function static_method($arg) {
	return $arg . " !";
  }
  public function object_method($arg) {
	return "[$arg]";
  }
}
$res = function($str) {
       	return substr($str, 0, 5);
    	};
   	 
callable_method($res , "Good Morning");      // anonymous method
callable_method("strtoupper", "Good Morning");      // string having method name
callable_method(["demoClass", "static_method"], "Good Morning");  // static class  // method
$object = new demoClass();  // Object method
callable_method([$object, "object_method"], "Good Morning");
                           ?> 

4. ITERABLE : Iterable is a pseudo-type which was introduced in PHP 7.1. It accepts an array or an object which is further iterable through foreach statement. The iterable keyword can be used as an argument or as a return type of the function.

<?php
                              function iterable_method(iterable $iterable_var) {
  			foreach($iterable_var as $val) {
			        echo $val." <br> ";
                                    } 
                              }
     $arr = ["sunday", "monday", "tuesday"];
                             iterable_method($arr);
                        ?> 

Special Data Type

RESOURCE and NULL are two special data types.

1. NULL : This data type has only one value i.e. NULL. If any variable is created without a value, it is automatically assigned with NULL value.

<?php
                    $var = "Hello";
                                $var = null;
                                var_dump($var);
              ?>

2. RESOURCE : This data type refers to an external resource. Resource variable acts as a reference to an external source of data such as stream, file etc. The function is_resource() can be used to determine if a variable is a resource and get_resource_type() will return the type of resource it is. Various types of resources are handled in PHP and some selected ones are listed below :

Resource Type NameCreated ByDestroyed ByDefinition
AddressInfosocket_addrinfo_lookup()NoneAddressInfo (sockets extension)
bzip2bzopen()bzclose()bzip2 file
curlcurl_copy_handle(), curl_init()curl_close()cURL handle
dbasedbase_open()dbase_close()Link to Dbase database
ftpftp_connect(), ftp_ssl_connect()ftp_close()FTP stream
imapimap_open()imap_close()Link to IMAP, POP3 server
mysql linkmysql_connect()mysql_close()Link to MySQL database
mysql resultmysql_db_query(), mysql_query()mysql_free_result()MySQL result
odbc linkodbc_connect()odbc_close()Link to ODBC database
Socketsocket_accept(), socket_create()socket_close()Socket (sockets extension)
streamopendir()closedir()Dir handle
streamfopen(), tmpfile()fclose()File handle
xmlxml_parser_create(), xml_parser_create_ns()xml_parser_free()XML parser
zlibgzopen()gzclose()gz-compressed file

2. RESOURCE : This data type refers to an external resource. Resource variable acts as a reference to an external source of data such as stream, file etc. The function is_resource() can be used to determine if a variable is a resource and get_resource_type() will return the type of resource it is. Various types of resources are handled in PHP and some selected ones are listed below :

<?php
                     $file = fopen("file.txt", "w");
                     echo get_resource_type($file) . "\n";
              ?>