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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
// Use var_dump() to verify the data type
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "25 km"; // String
$d = "km 25"; // String
$e = "hello"; // String
$f = true; // Boolean
$g = NULL; // NULL
$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "25 km"; // String
$d = "km 25"; // String
$e = "hello"; // String
$f = true; // Boolean
$g = NULL; // NULL
$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
$g = (float) $g;
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = 0; // Integer
$d = -1; // Integer
$e = 0.1; // Float
$f = "hello"; // String
$g = ""; // String
$h = true; // Boolean
$i = NULL; // NULL
$a = (bool) $a;
$b = (bool) $b;
$c = (bool) $c;
$d = (bool) $d;
$e = (bool) $e;
$f = (bool) $f;
$g = (bool) $g;
$h = (bool) $h;
$i = (bool) $i;
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;
?>
Convert object into array
Here a Car object becomes an array, and var_dump() shows its two properties as keys:
Output: PHP script
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("red", "Volvo");
$myCar = (array) $myCar;
var_dump($myCar);
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;
?>
Convert arrays into objects
One indexed array and one associative array become objects. Again there is no output:
Output: PHP script
<?php
$a = array("Volvo", "BMW", "Toyota"); // indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // associative array
$a = (object) $a;
$b = (object) $b;
?>
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:
Output: PHP script
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = NULL;
$b = NULL;
$c = NULL;
$d = NULL;
$e = NULL;
?>