Change Data Type
Casting in PHP is performed using the following statements:
- (string): Converts to the String data type
- (int): Converts to the Integer data type
- (float): Converts to the Float data type
- (bool): Converts to the Boolean data type
- (array): Converts to the Array data type
- (object): Converts to the Object data type
- (unset): Converts to NULL
Cast to String
To cast to a 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);
|
Cast to Integer
To cast to an integer, use the (int) statement:
Example
$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;
|
Cast to Float
To cast to a float, use the (float) statement:
Example
$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;
|
Cast to Boolean
To cast to a boolean, use the (bool) statement:
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;
|
If a value is 0, NULL, false, or empty, the (bool)
cast will convert it to false; otherwise, it will convert to true.
Notably, -1 will evaluate to true.
Cast to Array
To cast to an array, use the (array) statement:
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;
|
When converting to arrays, most data types become indexed arrays with a single element.
NULL values are converted to an empty array.
Objects are transformed into associative arrays, where property names become the keys and property values become the corresponding values.
Example
Converting Objects to 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);
|
Cast to Object
To cast to an object, use the (object) statement:
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;
|
When converting to objects, most data types become objects with a single property named “scalar,” holding the corresponding value.
NULL values are converted to an empty object.
Indexed arrays are transformed into objects where the index numbers serve as property names and the values serve as property values.
Associative arrays become objects with keys as property names and values as property values.
Example
Converting Arrays to Objects:
$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
To cast to NULL, use the (unset)
statement:
Example
$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;
|