Skip to main content

PHP Type Casting

Type casting means deliberately converting a value to another data type, for example turning a float into an integer. Instead of leaving the decision to PHP, you choose the type a variable ends up with.

The syntax is short: write the target type in parentheses right before the variable or value.

These are the casting operators:

  • (string) - gives a String
  • (int) - gives an Integer
  • (float) - gives a Float
  • (bool) - gives a Boolean
  • (array) - gives an Array
  • (object) - gives an Object
  • (unset) - deprecated; gives NULL

Cast to String

Put (string) in front of a variable or value to turn it into a string.

Cast to string example

Five values of different types become strings, and var_dump() confirms the result. Notice that true turns into "1" and NULL into an empty string:

Prompt
Write a PHP script that creates variables of different types (integer 5, float 5.34, string "hello", boolean true, NULL) and casts each to string using (string), then uses var_dump() to verify.
Output: PHP script

Cast to Integer

An integer is what you get when (int) comes right before a variable or value.

Cast to integer example

Here, values of several types are cast to integer. The script only does the conversions, so it prints nothing:

Prompt
Write a PHP script that creates variables of different types (integer, float, strings, boolean, NULL) and casts each to integer using (int).
Output: PHP script

Cast to Float

For a float, write (float) in front of the variable or value.

Cast to float example

Again the values are only converted, this time to float, so nothing is printed:

Prompt
Write a PHP script that creates variables of different types and casts each to float using (float).
Output: PHP script

Cast to Boolean

Writing (bool) before a variable or value gives you a boolean.

The outcome is false for 0, NULL, false and anything empty. Everything else becomes true, and that includes negative numbers such as -1.

Cast to boolean example

Nine values are cast to boolean here. Only $c, $g and $i become false, although the script prints nothing:

Prompt
Write a PHP script that creates variables of different types including 0, -1, 0.1, empty string, and NULL, then casts each to boolean using (bool).
Output: PHP script

Cast to Array

Use (array) before a variable or value to get an array.

The result depends on what you start with. Most types become an indexed array holding a single element, while NULL becomes an empty array.

An object becomes an associative array: each property name turns into a key, and each property value into that key's value.

Cast to array example

Each of these values becomes an array. No output appears, because the script never prints them:

Prompt
Write a PHP script that creates variables of different types and casts each to array using (array).
Output: PHP script

Convert object into array

Here a Car object becomes an array, and var_dump() shows its two properties as keys:

Prompt
Write a PHP script that defines a Car class with color and model properties, creates an instance, casts it to array, and uses var_dump() to display the result.
Output: PHP script

Cast to Object

Use (object) before a variable or value to get an object.

Most types end up as an object with a single property called "scalar", which holds the original value. NULL gives an empty object.

Arrays map neatly onto properties. In an indexed array, each index number becomes a property name; in an associative array, each key does. Either way, the array's values become the property values.

Cast to object example

Each of these values becomes an object. The script prints nothing:

Prompt
Write a PHP script that creates variables of different types and casts each to object using (object).
Output: PHP script

Convert arrays into objects

One indexed array and one associative array become objects. Again there is no output:

Prompt
Write a PHP script that creates an indexed array and an associative array, then casts both to objects using (object).
Output: PHP script

Cast to NULL

Note: PHP 7.2.0 deprecated the (unset) cast, and PHP 8.0.0 removed it.

To get NULL today, simply assign NULL to the variable:

Cast to NULL example

Whatever type each variable held before, it is NULL after this assignment. Nothing is printed:

Prompt
Write a PHP script that creates variables of different types and sets each to NULL.
Output: PHP script