Skip to main content

PHP Data Types

A variable can hold many kinds of data, and what you can do with a value depends on its type. PHP works with these types: String, Integer, Float (also known as double), Boolean, Array, Object, NULL, Resource.


PHP String

Strings hold text: a series of characters such as "Hello world!". Wrap the text in single or double quotes, and PHP treats it as a string.

String example

These two variables hold the same text, written once with double quotes and once with single quotes:

Prompt
Write a PHP script that creates a variable $x with value "Hello world!" and a variable $y with value 'Hello world!'. Use echo to display both variables, each on a separate line.
Output: PHP script

PHP Integer

Integers are whole numbers with no decimal part. On 32-bit systems they range from -2,147,483,648 to 2,147,483,647. When writing one, you need at least one digit and no decimal point, and the value may be positive or negative. You can write it as decimal (base 10), hexadecimal (base 16, starting with 0x), or octal (base 8, starting with 0).

To see a value's type alongside the value itself, use var_dump().

Integer example

Here var_dump() confirms that $x is an integer:

Prompt
Write a PHP script that creates a variable $x set to 5985. Use var_dump to display the data type and value of $x.
Output: PHP script

PHP Float

Floats, short for floating point numbers, have a decimal point or are written in exponential form. Once again, var_dump() can show both the type and the value.

Float example

Here var_dump() confirms that $x is a float:

Prompt
Write a PHP script that creates a variable $x set to 10.365. Use var_dump to display the data type and value of $x.
Output: PHP script

PHP Boolean

A Boolean has only two possible values, TRUE and FALSE. You will mostly meet them in conditions, where your code decides what to do next.

Boolean values

Here one variable gets each boolean value:

index.php
<?php
$x = true;
$y = false;
?>

PHP Array

With an array, a single variable can hold several values at once. As with the other types, var_dump() reveals the type and the contents.

Array example

The output lists the array's length and every element with its index, type and value:

Prompt
Write a PHP script that creates a variable $cars as an array containing "Volvo", "BMW", and "Toyota". Use var_dump to display the contents of the array.
Output: PHP script

PHP Object

Object-oriented programming is built on two ideas: classes and objects. Think of a class as a template and an object as one instance made from it. Every object gets the properties and behaviors defined in the class, yet each can hold its own property values.

Give a class a __construct() function, and PHP calls it for you every time you create an object from that class.

Object example

The Car class serves here as the template for three objects, each with its own color and model:

Prompt
Write a PHP script that defines a class Car with properties $color and $model, and a __construct function that sets those properties. Create an object from the class with color "red" and model "Volvo". Use echo to display the car's color and model.
Output: PHP script

PHP NULL Value

The Null data type is unusual because it has just one possible value: NULL. It means that a variable has no value assigned. PHP gives this value automatically to a variable created without one, and you can also set it yourself to empty a variable.

NULL example

$x starts with a string and is then emptied, so var_dump() reports NULL:

Prompt
Write a PHP script that creates a variable $x with value "Hello world!" then sets it to NULL. Use var_dump to display the data type and value of $x.
Output: PHP script

Change Data Type

A PHP variable's type follows its value. Assign an integer and the variable is an integer; assign a string to that same variable later and it becomes a string. To change the type yourself, use PHP's casting feature or the settype() function.

Casting to change type

Here $x starts as an integer and a cast turns it into a string:

Prompt
Write a PHP script that creates a variable $x set to 5. Cast it to a string using (string) and store in $x. Use var_dump to display the data type and value of $x.
Output: PHP script