PHP Data Types

Summary: in this tutorial, you will learn about PHP data types and how to use them effectively in the web application.

The data type specifies the amount of memory that allocates to the variable associated with it. In addition, the data type determines the operations that you can perform on it.

PHP provides all basic data types including scalar, compound, and special.

Scalar data types

A variable is a scalar when it holds a single value. PHP has four scalar data types including integer, float, string and bool.

Integers are whole number that defined in the set {…-3,-2-,-1,0,1,2,3…}.  The size of the integer depends on the platform where PHP runs on. The constant PHP_INT_SIZE holds the size of the PHP integer on a specific platform. In PHP, integer type is denoted as int

Floats are floating point numbers, which are also known as floats, doubles or real numbers.  The following example illustrates the floating point numbers in PHP:

It is noticed that floating point numbers have limited precision. PHP uses IEEE 754 double format.

Boolean is the simplest data type in PHP that has only two values TRUE and FALSE, both of them are case-insensitive. It means true and TRUE is the same value.

In PHP, the following values are also considered as the Boolean value of FALSE when you use them in the conditional statements e.g. if else or switch case with logical operators:

  • Integer 0 and float 0.00
  • String “0” or an empty string
  • An array that contains zero element
  • Special type  NULL
  • Unset variables
  • A SimpleXML object that created from an empty tag

A string is a sequence of characters. You will learn more about PHP string in the later tutorial.

Compound data types

Compound data is the data that can contain more than one value. PHP provides two compound data types including array and object. We will discuss array in PHP array tutorial and object in the PHP object tutorial.

Special data types

There are two special data types in PHP that have special meanings:

  • Resource: hold a reference to an external resource, e.g. file or database connection.
  • Null: If a variable is null, it means the variable does not contain any value.

In this tutorial, we’ve discussed the basic PHP data types so that you can use each of them to manipulate data in your application.

  • Was this tutorial helpful ?
  • YesNo