Sometimes you need to convert a variable from one data type to another, or you want a variable to have a specific data type. This process is known as casting.
Change Data Type
Casting in PHP is done with these statements:
(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)
– Converts to data type NULL
Cast to String
To cast a variable to a string, use the (string)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (string) $gear;
$speed = (string) $speed;
$greeting = (string) $greeting;
$isFast = (string) $isFast;
$notAvailable = (string) $notAvailable;
// To verify the type of any object in PHP, use the var_dump() function:
var_dump($gear);
var_dump($speed);
var_dump($greeting);
var_dump($isFast);
var_dump($notAvailable);
Cast to Integer
To cast a variable to an integer, use the (int)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$distance = "500 miles"; // String
$road = "miles 500"; // String
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (int) $gear;
$speed = (int) $speed;
$distance = (int) $distance;
$road = (int) $road;
$greeting = (int) $greeting;
$isFast = (int) $isFast;
$notAvailable = (int) $notAvailable;
Cast to Float
To cast a variable to a float, use the (float)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$distance = "500 miles"; // String
$road = "miles 500"; // String
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (float) $gear;
$speed = (float) $speed;
$distance = (float) $distance;
$road = (float) $road;
$greeting = (float) $greeting;
$isFast = (float) $isFast;
$notAvailable = (float) $notAvailable;
Cast to Boolean
To cast a variable to a boolean, use the (bool)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$zero = 0; // Integer
$negative = -1; // Integer
$distance = 0.1; // Float
$greeting = "hello"; // String
$emptyString = ""; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (bool) $gear;
$speed = (bool) $speed;
$zero = (bool) $zero;
$negative = (bool) $negative;
$distance = (bool) $distance;
$greeting = (bool) $greeting;
$emptyString = (bool) $emptyString;
$isFast = (bool) $isFast;
$notAvailable = (bool) $notAvailable;
If a value is 0, NULL, false, or empty, casting it to a boolean converts it to false
. Otherwise, it converts to true
. Even -1 converts to true
.
Cast to Array
To cast a variable to an array, use the (array)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (array) $gear;
$speed = (array) $speed;
$greeting = (array) $greeting;
$isFast = (array) $isFast;
$notAvailable = (array) $notAvailable;
Most data types convert into an indexed array with one element when cast to arrays. NULL values convert to an empty array.
Example: Converting Objects into Arrays
class Motorcycle {
public $brand;
public $model;
public function __construct($brand, $model) {
$this->brand = $brand;
$this->model = $model;
}
public function description() {
return "My motorcycle is a " . $this->brand . " " . $this->model . "!";
}
}
$myMotorcycle = new Motorcycle("Honda", "CB500");
$myMotorcycle = (array) $myMotorcycle;
var_dump($myMotorcycle);
Cast to Object
To cast a variable to an object, use the (object)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (object) $gear;
$speed = (object) $speed;
$greeting = (object) $greeting;
$isFast = (object) $isFast;
$notAvailable = (object) $notAvailable;
Most data types convert into an object with one property named “scalar” with the corresponding value. NULL values convert to an empty object.
Example: Converting Arrays into Objects
$brands = array("Honda", "Yamaha", "Suzuki"); // Indexed array
$models = array("CB500"=>"500cc", "R6"=>"600cc", "GSX"=>"750cc"); // Associative array
$brands = (object) $brands;
$models = (object) $models;
Cast to NULL
To cast a variable to NULL, use the (unset)
statement:
$gear = 5; // Integer
$speed = 120.5; // Float
$greeting = "hello"; // String
$isFast = true; // Boolean
$notAvailable = NULL; // NULL
$gear = (unset) $gear;
$speed = (unset) $speed;
$greeting = (unset) $greeting;
$isFast = (unset) $isFast;
$notAvailable = (unset) $notAvailable;