PHP Type Casting
PHP Type Casting
PHP type casting is an explicit process of converting a value from one data type to another, such as from a float to an integer.
This gives the developer direct control over the data type of a variable.
You cast a variable by placing the cast type within parentheses before the variable or value you want to convert.
The PHP casting operators are:
(string)- Converts to data type String(int)- Converts to data type Integer(float)- Converts to data type Float(bool)- Converts to data type Boolean(array)- Converts to data type Array(object)- Converts to data type Object(unset)- Deprecated. Converts to data type NULL
Cast to String
To cast to data type string: Use (string)
before the variable or value to convert:
Example
$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);
Try it Yourself »
Cast to Integer
To cast to data type integer: Use (int)
before the variable or value to convert:
Example
$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;
Try it Yourself »
Cast to Float
To cast to data type float: Use (float)
before the variable or value to convert:
Example
$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;
Try it Yourself »
Cast to Boolean
To cast to data type boolean: Use (bool)
before the variable or value to convert:
Example
$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;
Try it Yourself »
If a value is 0, NULL, false, or empty, (bool) converts it to false, otherwise true. Even -1 converts to true.
Cast to Array
To cast to data type array: Use (array)
before the variable or value to convert:
Example
$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;
Try it Yourself »
When casting to array, most data types converts into an indexed array with one element.
NULL values converts into an empty array object.
An object converts into an associative array where the property names becomes the keys and the property values becomes the values:
Example
Convert object into array:
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);
Try it Yourself »
Cast to Object
To cast to data type object: Use (object)
before the variable or value to convert:
Example
$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;
Try it Yourself »
When casting to object, most data types converts into an object with one property, named "scalar", with the corresponding value.
NULL values converts to an empty object.
Indexed arrays converts to objects with the index number as property name and the value as property value.
Associative arrays converts into objects with the keys as property names and values as property values.
Example
Convert arrays into objects:
$a = array("Volvo", "BMW", "Toyota"); // indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // associative array
$a = (object) $a;
$b = (object) $b;
Try it Yourself »Cast to NULL
Note: The (unset) statement was deprecated in PHP 7.2.0, and removed in PHP 8.0.0.
To cast to NULL, set the variable to NULL:
Example
$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;
Try it Yourself »