This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
Sometimes you need to change a variable from one data type into another, and sometimes you want a variable to have a specific data type. This can be done with casting.
To cast to string, use the (string) statement:
$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; //To verify the type of any object in PHP, use the var_dump() function: var_dump($a); var_dump($b); var_dump($c); var_dump($d); var_dump($e);
To cast to integer, use the (int) statement:
$a = 5; // Integer $b = 5.34; // Float $c = "25 kilometers"; // String $d = "kilometers 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;
To cast to float, use the (float) statement:
$a = 5; // Integer $b = 5.34; // Float $c = "25 kilometers"; // String $d = "kilometers 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;
To cast to boolean, use the (bool) statement:
$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;
To cast to array, use the (array) statement:
$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;
Converting Objects into Arrays:
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);
To cast to object, use the (object) statement:
$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;
Converting 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;
To cast to NULL, use the (unset) statement:
$a = 5; // Integer $b = 5.34; // Float $c = "hello"; // String $d = true; // Boolean $e = NULL; // NULL $a = (unset) $a; $b = (unset) $b; $c = (unset) $c; $d = (unset) $d; $e = (unset) $e;
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Module 2 quiz