What is type casting in PHP ?

Type casting is the process of converting one data type to another. Stated differently, there are situations when a variable must be converted from one data type to another and instances when a certain data type is desired for a variable. Casting is one way to do this.

Type Casting in PHP is done with these statements:

1 – (string) – changes to the String data type
2 – (int) – changes to the Integer data type
3 – (float) – changes to the Float data type
4 – (bool) – changes to the Boolean data type
5 – (array) – changes to the Array data type
6 – (object) – changes to the Object data type
7 – (unset) – changes to the NULL data type

1 – Cast to String : To cast to string, use the (string) statement:

Example –
$var = 45; // Integer
$b = 9.34; // Float
$c = “good morning”; // String
$d = true; // Boolean
$e = NULL; // NULL

$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;

Note :- It should be noted that a Boolean is transformed to the value “1” when cast into a string, while an empty string (“”) is produced when casting NULL into a string.

2 – Cast to Integer : To cast to 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
var_dump($g);

output –
int(5)
int(5)
int(25)
int(0)
int(0)
int(1)
int(0)

Note :- Keep in mind that the (int) function utilizes the number that appears at the beginning of the string when casting it. The (int) function converts strings into the number 0 if it does not begin with a number.

3 – Cast to Float: To cast to 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
var_dump($g);

output –
float(5)
float(5.34)
float(25)
float(0)
float(0)
float(1)
float(0)

Note :- Observe that the (float) function use the number that appears at the beginning of the string when casting it. In the event that a string does not begin with a number, the (float) function converts it to 0.

4 – Cast to Boolean : To cast to 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
var_dump($g);

output –
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)

Note :-

If a value is 0, NULL, false, or empty, the (bool) converts it into false, otherwise true.

Even -1 converts to true.

5 – Cast to Array : 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);

output
array(1) {
[0]=>
int(5)
}
array(1) {
[0]=>
float(5.34)
}
array(1) {
[0]=>
int(0)
}
array(1) {
[0]=>
int(-1)
}
array(1) {
[0]=>
float(0.1)
}
array(1) {
[0]=>
string(5) “hello”
}
array(1) {
[0]=>
string(0) “”
}
array(1) {
[0]=>
bool(true)
}
array(0) {
}

Note :- Most data types change to an indexed array with a single element when they are converted to arrays.
An empty array object is created when NULL values are used.

When objects are converted into associative arrays, their property values become the values and their property names become the keys:

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);

output –
array(2) {
[“color”]=>
string(3) “red”
[“model”]=>
string(5) “Volvo”
}

Note :- Property names become the keys and property values become the values when objects are converted into associative arrays.

6 – Cast to Object : To cast to 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);

output –
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);​

Output:​
object(stdClass)#1 (1) {
[“scalar”]=>
int(5)
}
object(stdClass)#2 (1) {
[“scalar”]=>
float(5.34)
}
object(stdClass)#3 (1) {
[“scalar”]=>
string(5) “hello”
}
object(stdClass)#4 (1) {
[“scalar”]=>
bool(true)
}
object(stdClass)#5 (0) {
}

Note :-

  • Most data types have one property—named “scalar”—and a corresponding value when they convert into objects.
  • The empty object is the result of using NULL values.
  • The index number serves as the property name and the value serves as the property value when converting index-based arrays into objects.
  • Property names and values are the keys and values, respectively, that associative arrays translate into objects.

Arrays to Objects Conversion Example:

$a = array(“Toyota”, “Volvo”, “BMW”);~/indexed array
$b= array(“Joe”=>”43”, “Ben”=>”37”, “Peter”=>”35”); associative array

Let $a = (object) $a and $b = (object) $b;

var_dump($a);
var_dump($b);

output –
object(stdClass)#1 (3) {
[0]=>
string(5) “Volvo”
[1]=>
string(3) “BMW”
[2]=>
string(6) “Toyota”
}
object(stdClass)#2 (3) {
[“Peter”]=>
string(2) “35”
[“Ben”]=>
string(2) “37”
[“Joe”]=>
string(2) “43”
}

7 – 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);

output –
NULL
NULL
NULL
NULL
NULL