PHP Type Casting: A Complete Guide
In PHP, variables are loosely typed. However, there are situations where explicit data type conversion—known as type casting—is essential. This tutorial from Devyra explains how to convert variables between types such as string, integer, float, boolean, array, object, and NULL.
How to Cast Data Types in PHP
PHP supports the following casting operators:
(string)
– Converts to String(int)
– Converts to Integer(float)
– Converts to Float (double)(bool)
– Converts to Boolean(array)
– Converts to Array(object)
– Converts to Object(unset)
– Converts to NULL
Cast to String
$a = 5;
$b = 5.34;
$c = "hello";
$d = true;
$e = NULL;
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
var_dump($a, $b, $c, $d, $e);
All values are converted to string representations. Booleans become "1"
or ""
; NULL
becomes an empty string.
Cast to Integer
$a = 5;
$b = 5.34;
$c = "25 kilometers";
$d = "kilometers 25";
$e = "hello";
$f = true;
$g = NULL;
$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;
var_dump($a, $b, $c, $d, $e, $f, $g);
PHP parses integers from strings starting with a digit. Invalid strings cast to 0
.
Cast to Float
$a = 5;
$b = 5.34;
$c = "25 kilometers";
$d = "kilometers 25";
$e = "hello";
$f = true;
$g = NULL;
$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
$g = (float) $g;
var_dump($a, $b, $c, $d, $e, $f, $g);
Similar to integers, PHP parses floats from strings with leading numeric values.
Cast to Boolean
$a = 5;
$b = 5.34;
$c = 0;
$d = -1;
$e = 0.1;
$f = "hello";
$g = "";
$h = true;
$i = 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;
var_dump($a, $b, $c, $d, $e, $f, $g, $h, $i);
Values considered “empty” (like 0
, ""
, NULL
, or false
) convert to false
. Everything else, including -1
, becomes true
.
Cast to Array
$a = 5;
$b = 5.34;
$c = "hello";
$d = true;
$e = NULL;
$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;
var_dump($a, $b, $c, $d, $e);
Most values convert into indexed arrays with a single element. NULL
becomes an empty array.
Convert Object to Array
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
}
$myCar = new Car("red", "Volvo");
$myCar = (array) $myCar;
var_dump($myCar);
Objects convert to associative arrays where property names become keys.
Cast to Object
$a = 5;
$b = 5.34;
$c = "hello";
$d = true;
$e = NULL;
$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;
var_dump($a, $b, $c, $d, $e);
Most primitive types convert to an object with a scalar
property. NULL
becomes an empty object.
Convert Array to Object
$a = array("Volvo", "BMW", "Toyota"); // Indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // Associative array
$a = (object) $a;
$b = (object) $b;
var_dump($a, $b);
Indexed arrays become objects with numeric property names. Associative arrays preserve key-value pairs as object properties.
Cast to NULL
To convert any value to NULL
, use the (unset)
cast:
$a = 5;
$b = 5.34;
$c = "hello";
$d = true;
$e = NULL;
$a = (