PHP Tutorial PHP Advanced PHP References

PHP - Data Types

One of the most important parts of learning any programming language is to understand what are the available data types, and how data is stored, accessed and manipulated in that language. In PHP, different types of data can be stored using variables. PHP supports the following data types:

Data TypesDescription
IntegersNon-decimal number between -2,147,483,648 and 2,147,483,647.
Doubles Floating-point numbers like 10.5.
BooleanTwo possible values either true or false.
NULLis a special type that only has one value: NULL.
StringsUsed to store text or sequence of characters like "This is a string".
Arrayare named and indexed collections of other values.
Objectare instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
Resourceare special variables that hold references to resources external to PHP (such as database connections).

Array, Object and Resource data types are discussed in later section of this tutorial. In this section, we will discuss the remaining data types of PHP.

PHP Integers

Integers are whole numbers without any decimal value, like 545. It can be a positive or negative number. Consider the example below:

$x = 1000;
$y = -3;
$z = $x * $y;

echo $z;

The output of the above code will be:


Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format. Decimal format is the default, octal integers are specified with a leading 0, and hexadecimals have a leading 0x. For most common platforms, the range of integer data type is -2,147,483,648 and 2,147,483,647.

PHP Doubles

Doubles are the floating point numbers like 10.5. By default, Doubles print with the minimum number of decimal places required. Consider the example below:

$x = 10.555500;
$y = 25.34450000;
$z = $x + $y;

echo "$x + $y = $z";

The output of the above code will be:

10.5555 + 25.3445 = 35.9

PHP Booleans

Booleans have two possible values either true or false. A boolean value can be created by assigning TRUE / FALSE to a variable. Along with this, it is also a usual return type when a comparison operator is used. Consider the example below:

$x = 50;

//conditional statement returns TRUE
if($x > 10) 
  echo "$x is greater than 10.\n"; 
  echo "$x is NOT greater than 10.\n";

The output of the above code will be:

50 is greater than 10.

Interpreting other types as Booleans

When other types is used in a Boolean context the following points need to be remembered:

  • All numbers except zero are true. zero is false.
  • All strings are true except empty string (zero characters) and "0" are false.
  • Values of type NULL are always false.
  • An array is false if it is empty, and true otherwise.
  • SimpleXML objects created from elements which have neither children nor attributes is false, and true otherwise.


NULL is a special data type in PHP which has only one value which is NULL. To give a variable the NULL value, simply assign it like this:

$var = NULL;

The special constant NULL is capitalized by convention, but actually it is case insensitive and can also be written as:

$var = null;

A variable that has been assigned NULL has the following properties:

  • Evaluates to FALSE in a Boolean context.
  • Returns FALSE when tested with IsSet() function.

PHP Strings

Strings are one of the most common data types in PHP. It is used for storing text. A string can be any text inside single or double quotes. Consider the example below:

$x = "HELLO WORLD!.";
$y = 'Hello World!.';

echo $x."\n";
echo $y;

The output of the above code will be:

Hello World!.

Special Strings in String

The backslash \ escape character is used to convert special character like double quote, and new line, etc. into the string character. The below mentioned table describes special characters in PHP:

Escape CharacterResultExample
\"""\"Hello\"" is converted into: "Hello"
\$$"\$50" is converted into: $50
\\\"A\\C" is converted into: A\C
\nnew line"Hello\nJohn" is converted into:
\tTab"Hello\tMarry" is converted into: Hello    Marry
\rCarriage return"Hello\rJohn" is converted into: