At times, there's a necessity to alter a variable from one data type to another, or to ensure that a variable possesses a specific data type. This can be achieved through casting.
Change Data Type
In PHP, casting is performed using these statements:
(string)
- Converts to String data type(int)
- Converts to Integer data type(float)
- Converts to Float data type(bool)
- Converts to Boolean data type(array)
- Converts to Array data type(object)
- Converts to Object data type(unset)
- Converts to NULL data type
Cast to String
To convert to a string, you can use the (string)
statement.
<?php
$number = 42; // Integer
$decimal = 3.14; // Float
$text = "hello"; // String
$flag = true; // Boolean
$empty = null; // NULL
// Casting the variables to strings
$number = (string) $number;
$decimal = (string) $decimal;
$text = (string) $text;
$flag = (string) $flag;
$empty = (string) $empty;
// Using var_dump() to display the type and value of each variable
var_dump($number); // string(2) "42"
var_dump($decimal); // string(4) "3.14"
var_dump($text); // string(5) "hello"
var_dump($flag); // string(1) "1"
var_dump($empty); // string(0) ""
?>
Cast to Integer
To convert to an integer, utilize the (int)
statement.
<?php
$num = 12; // Integer
$decimalNumber = 7.89; // Float
$distance = "50 miles"; // String
$location = "miles 50"; // String
$greeting = "welcome"; // String
$isActive = false; // Boolean
$unknown = null; // NULL
// Casting the variables to integers
$num = (int) $num; // 12 remains 12
$decimalNumber = (int) $decimalNumber; // 7.89 becomes 7
$distance = (int) $distance; // "50 miles" becomes 50
$location = (int) $location; // "miles 50" becomes 0
$greeting = (int) $greeting; // "welcome" becomes 0
$isActive = (int) $isActive; // false becomes 0
$unknown = (int) $unknown; // null becomes 0
// Using var_dump() to display the type and value of each variable
var_dump($num); // int(12)
var_dump($decimalNumber); // int(7)
var_dump($distance); // int(50)
var_dump($location); // int(0)
var_dump($greeting); // int(0)
var_dump($isActive); // int(0)
var_dump($unknown); // int(0)
?>
Cast to Float
To cast to float, use the (float)
statement:
<?php
$integerValue = 15; // Integer
$floatValue = 12.34; // Float
$measurement = "45 kilometers"; // String
$description = "kilometers 45"; // String
$phrase = "hello world"; // String
$isActive = true; // Boolean
$emptyValue = null; // NULL
// Casting the variables to floats
$integerValue = (float) $integerValue; // 15 becomes 15.0
$floatValue = (float) $floatValue; // 12.34 remains 12.34
$measurement = (float) $measurement; // "45 kilometers" becomes 45.0
$description = (float) $description; // "kilometers 45" becomes 0.0
$phrase = (float) $phrase; // "hello world" becomes 0.0
$isActive = (float) $isActive; // true becomes 1.0
$emptyValue = (float) $emptyValue; // null becomes 0.0
// Using var_dump() to display the type and value of each variable
var_dump($integerValue); // float(15)
var_dump($floatValue); // float(12.34)
var_dump($measurement); // float(45)
var_dump($description); // float(0)
var_dump($phrase); // float(0)
var_dump($isActive); // float(1)
var_dump($emptyValue); // float(0)
?>
Cast to Boolean
To convert to a boolean, you can use the (bool)
statement.
<?php
$number1 = 10; // Integer
$number2 = 3.14; // Float
$quantity = -5; // Integer
$flag = 0; // Integer
$ratio = 0.0; // Float
$message = "hello"; // String
$emptyString = " "; // String with a space
$isActive = false; // Boolean
$nothing = null; // NULL
// Casting the variables to booleans
$number1 = (bool) $number1; // 10 becomes true
$number2 = (bool) $number2; // 3.14 becomes true
$quantity = (bool) $quantity; // -5 becomes true
$flag = (bool) $flag; // 0 becomes false
$ratio = (bool) $ratio; // 0.0 becomes false
$message = (bool) $message; // "hello" becomes true
$emptyString = (bool) $emptyString; // " " becomes true (non-empty string)
$isActive = (bool) $isActive; // false remains false
$nothing = (bool) $nothing; // null becomes false
// Using var_dump() to display the type and value of each variable
var_dump($number1); // bool(true)
var_dump($number2); // bool(true)
var_dump($quantity); // bool(true)
var_dump($flag); // bool(false)
var_dump($ratio); // bool(false)
var_dump($message); // bool(true)
var_dump($emptyString); // bool(true)
var_dump($isActive); // bool(false)
var_dump($nothing); // bool(false)
?>
The (bool)
statement converts a value into false if it's 0, NULL, false, or empty. Otherwise, it's converted to true. Even -1 is converted to true.
Cast to Array
To convert to an array, you can use the (array)
statement.
<?php
$integerValue = 42; // Integer
$floatValue = 1.618; // Float
$text = "world"; // String
$isActive = false; // Boolean
$nothing = "empty"; // String
// Casting the variables to arrays
$integerValue = (array) $integerValue; // [42]
$floatValue = (array) $floatValue; // [1.618]
$text = (array) $text; // ["world"]
$isActive = (array) $isActive; // [false]
$nothing = (array) $nothing; // ["empty"]
// Using var_dump() to display the type and value of each variable
var_dump($integerValue); // array(1) { [0]=> int(42) }
var_dump($floatValue); // array(1) { [0]=> float(1.618) }
var_dump($text); // array(1) { [0]=> string(5) "world" }
var_dump($isActive); // array(1) { [0]=> bool(false) }
var_dump($nothing); // array(1) { [0]=> string(5) "empty" }
?>
During conversion to arrays, most data types transform into an indexed array with a single element.
NULL values become an empty array object.
Objects are converted into associative arrays where the property names become the keys, and the property values become the values.
Converting Objects into Arrays:
<?php
class Vehicle {
public $shade;
public $type;
public function __construct($shade, $type) {
$this->shade = $shade;
$this->type = $type;
}
}
$myVehicle = new Vehicle("blue", "Toyota");
// Cast object to an array
$myVehicleArray = (array) $myVehicle;
// Display the array representation of the object
var_dump($myVehicleArray);
?>
/*
Output:
array(2) { ["shade"]=> string(4) "blue" ["type"]=> string(6) "Toyota" }
*/
Cast to Object
To convert to an object, utilize the (object)
statement.
<?php
$number = 42; // Integer
$decimal = 3.14; // Float
$text = "hello"; // String
$flag = true; // Boolean
$empty = null; // NULL
// Casting the variables to objects
$number = (object) $number; // {scalar: 42}
$decimal = (object) $decimal; // {scalar: 3.14}
$text = (object) $text; // {scalar: "hello"}
$flag = (object) $flag; // {scalar: true}
$empty = (object) $empty; // {scalar: null}
// Using var_dump() to display the type and value of each variable
var_dump($number); // object(stdClass)#1 (1) { ["scalar"]=> int(42) }
var_dump($decimal); // object(stdClass)#2 (1) { ["scalar"]=> float(3.14) }
var_dump($text); // object(stdClass)#3 (1) { ["scalar"]=> string(5) "hello" }
var_dump($flag); // object(stdClass)#4 (1) { ["scalar"]=> bool(true) }
var_dump($empty); // object(stdClass)#5 (1) { ["scalar"]=> NULL }
?>
When converting to objects, most data types become an object with one property named "scalar
," containing the corresponding value.
NULL values are transformed into an empty object.
Indexed arrays are converted into objects with the index number as the property name and the value as the property value.
Associative arrays are converted into objects with the keys as property names and the values as property values.
Converting Arrays into Objects:
<?php
$carModels = array("Tesla", "BMW", "Audi"); // Indexed array
$employeeSalaries = array("John"=>"50000", "Jane"=>"60000", "Doe"=>"55000"); // Associative array
// Casting the arrays to objects
$carModels = (object) $carModels;
$employeeSalaries = (object) $employeeSalaries;
// Using var_dump() to display the type and value of each variable
var_dump($carModels); // object(stdClass)#1 (3) { [0]=> string(5) "Tesla" [1]=> string(3) "BMW" [2]=> string(4) "Audi" }
var_dump($employeeSalaries); // object(stdClass)#2 (3) { ["John"]=> string(5) "50000" ["Jane"]=> string(5) "60000" ["Doe"]=> string(5) "55000" }
?>
Cast to NULL
To cast to NULL, use the (unset)
statement:
<?php
$integerVal = 8; // Integer
$floatVal = 6.75; // Float
$stringVal = "welcome"; // String
$booleanVal = false; // Boolean
$nullVal = null; // NULL
// Converting to NULL datatype
$integerVal = (unset) $integerVal; // converts to datatype NULL
$floatVal = (unset) $floatVal; // converts to datatype NULL
$stringVal = (unset) $stringVal; // converts to datatype NULL
$booleanVal = (unset) $booleanVal; // converts to datatype NULL
$nullVal = (unset) $nullVal; // converts to datatype NULL
// Attempting to use var_dump() will show NULL
var_dump($integerVal); // Output: NULL
var_dump($floatVal); // Output: NULL
var_dump($stringVal); // Output: NULL
var_dump($booleanVal); // Output: NULL
var_dump($nullVal); // Output: NULL
?>