Data Types in PHP

PHP supports data types are-

  • boolean
  • integer
  • float (floating-point number, aka double)
  • string
  • array
  • object
  • resource
  • NULL


This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.


$isLogout = True; // assign the value TRUE to $isLogout


An integer is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}.
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. The negation operator can be used to denote a negative integer.


$num = 1234; // decimal number
$num = -123; // a negative number
$num = 0123; // octal number (equivalent to 83 decimal)
$num = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$num = 0b11111111; // binary number (equivalent to 255 decimal)

Floating point numbers-

Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:-


$x = 1.234;
$y = 1.2e3;
$z = 7E-10;

Note- The size of a float is platform-dependent, although a maximum of approximately 1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).


A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.
A string literal can be specified in four different ways:

  • single quoted
  • double quoted
  • heredoc syntax
  • nowdoc syntax


echo 'this is a simple string single quoted.'; // single quoted

echo "this is example of double quoted"; // double quoted

/************* Heredoc string quoting example **************/
$str = <<<EOD
Example of string using heredoc syntax.

/******************** Nowdoc string quoting example *************/
echo <<<'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.


An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
The key can either be an integer or a string. The value can be of any type. 


$array = array(
"name" => "robert",
"email" => "",
"country" => "USA",

// Indexed arrays without key

$array = array("php", "swift", "javascript", "erlang");

array(4) {
string(3) "php"
string(5) "swift"
string(10) "javascript"
string(6) "erlang"


To create a new object, use the new statement to instantiate a class. 


class person
function say_hello()
echo "say hello";

$pObj = new person;
$pObj-> say_hello();


A resource is a special variable, holding a reference to an external resource such as opened files, database connections, image canvas areas etc.


The special NULL value represents a variable with no value. NULL is the only possible value of type null.

A variable is considered to be null if:

  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().


$x = "Hello world!";
$x = null;


Back to Top