PHP Data Types

Since PHP is a loosely typed language, therefore whatever the value assigned to a variable, the variable becomes automatically of that type.

List of data types supported by PHP are:

  1. integer
  2. float (also called as double)
  3. string
  4. array
  5. boolean
  6. NULL
  7. object

Note - The gettype() function returns the data type of a variable or the value itself, specified as its parameter.

PHP integer Data Type

All non-decimal numbers between -2,147,483,648 and 2,147,483,647 are considered as an integer type in PHP. For example:

<?php
   echo gettype(5), "<BR>";
   
   $x = 12;
   echo gettype($x), "<BR>";
   
   $x = -48;
   echo gettype($x), "<BR>";
   
   $x = 0;
   echo gettype($x), "<BR>";
?>

The output produced by above PHP example is:

php data type integer type

To get data type along with value/information, use var_dump() function. Here is an example:

<?php
   echo var_dump(5), "<BR>";
   
   $x = 12;
   echo var_dump($x), "<BR>";
   
   $x = -48;
   echo var_dump($x), "<BR>";
   
   $x = 0;
   echo var_dump($x), "<BR>";
?>

Now the output produced by this PHP example is shown in the snapshot given below:

php data type integer

PHP float Data Type

A number having a decimal point or a number in exponential form, is considered as a floating-point type in PHP. For example:

<?php
   echo var_dump(5.3);
?>

The output should be float(5.3)

PHP string Data Type

A text/value enclosed within a single or a double quote, is considered as a string type in PHP. For example:

<?php
   echo var_dump('13'), "<BR>";
   echo var_dump("14.4"), "<BR>";
   echo var_dump("codescracker.com"), "<BR>";
?>

The snapshot given below shows the output produced by above PHP example on string data type:

php data type string

In above output, the value inside string() indicates the number of character available in the string. For example, since there are only two characters available in the string '13', therefore the output was string(2) "13" for first statement. Similar thing goes with second and third statement. Use gettype() to print only the type of value/variable.

PHP array Data Type

The array is used when we need to store multiple values in a single variable. For example:

<?php
   $x = array("Python", 32, "PHP");
   echo var_dump($x);
?>

The output is:

php data type array

In above output, array(3) indicates that the variable $x is of array type with total of 3 elements available or defined in/to it. Now inside the curly braces, the first one, that is [0]=> string(6) "Python" indicates that at 0th index, the element available is of string type whose length is 6 and value is Python. Similar thing goes with second and third element.

Let me tell you again, if you want to print, only the data type of a variable or a value, then better to go with gettype(). And to get the data type along with other information, use var_dump(). For example, if I use gettype() in place of var_dump() in above example, then the output should be array

PHP boolean Data Type

The two values that are of boolean type are true and false. For example:

<?php
   echo gettype(true);
   echo gettype(false);
?>

You will get boolean as/on output for both the statement.

PHP NULL Data Type

A variable without value, considered as a variable with a NULL value, because NULL automatically gets initialized to a variable with no value at all. Also, we can initialize NULL to an already defined variable to empty the variable. Here is an example on NULL data type in PHP:

<?php
   $x = NULL;
   echo gettype($x);
?>

The output should be NULL

PHP object Data Type

<?php
   class myClass{
   }
   $x = new myClass();
   echo gettype($x);
?>

The output should be object. The detailed description about classes and objects are described in its separate tutorial.

PHP Online Test


« Previous Tutorial Next Tutorial »