Explicit Casting
Explicit casting is done when you want to manually convert a variable from one type to another using explicit type casts.
Supported Data Types for Casting
(int) or (integer) : Converts to an integer.(bool) or (boolean) : Converts to a boolean (0 becomes false , anything else becomes true ).(float) , (double) , or (real) : Converts to a float.(string) : Converts to a string.(array) : Converts to an array.(object) : Converts to an object.(unset): Converts to data type NULL
1) Cast to Integer
When casting a float or string to an integer, the fractional part is removed (truncated), or if the string contains non-numeric characters, it will be converted to 0 .
Example
<pre >
<?php
$a = (int ) 24 ;
$b = (int ) 2.3 ;
$c = (int ) "25 apples" ;
$d = (int ) "apples 25" ;
$e = (int ) "hello" ;
$f = (int ) true ;
$g = (int ) NULL ;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
var_dump($g);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
2) Cast to Boolean
To cast to boolean, use the (bool) statement:
Example
< pre >
<?php
$a = (bool ) 25 ;
$b = (bool ) 25.4
$c = (bool ) 0 ;
$d = (bool ) -1 ;
$e = (bool ) 0.1 ;
$f = (bool ) hello";
$g = (bool ) "" ;
$h = (bool ) true ;
$i = (bool ) NULL ;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
var_dump($g);
var_dump($h);
var_dump($i);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
3) Cast to Float
When casting to a float, the value is treated as a decimal number.
Example
<pre >
<?php
$a = (float ) 25 ;
$b = (float ) 25.4 ;
$c = (float ) "25 apples" ;
$d = (float ) "apples 25" ;
$e = (float ) "hello" ;
$f = (float ) true ;
$g = (float ) NULL ;
//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);
var_dump($f);
var_dump($g);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
4) Cast to String
You can cast integers, floats, or booleans to strings.
Example
< pre >
<?php
$a = (string ) 25 ;
$b = (string ) 25.4 ;
$c = (string ) "hello" ;
$d = (string ) true ;
$e = (string ) NULL ;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
5) Type Casting in Arrays
PHP will not cast arrays to another data type unless you manually handle it with explicit casting.
Example
<pre >
<?php
$a = (array ) 5 ;
$b = (array ) 5.34 ;
$c = (array ) "hello" ;
$d = (array ) true ;
$e = (array ) NULL ;
//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);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Objects converts into associative arrays where the property names becomes the keys and the property values becomes the values.
Example
<pre >
<?php
class MyClass {
public $name = "John" ;
public $age = 30 ;
}
$obj = new MyClass();
$array = (array )$obj; // Casts the object to an array
print_r($array); // Outputs the object properties as an array
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
6) Type Casting in Objects
To cast to object, use the (object) statement.
Example
<pre >
<?php
$a = (object ) 25 ;
$b = (object ) 25.4 ;
$c = (object ) "hello" ;
$d = (object ) true ;
$e = (object ) NULL ;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Indexed arrays converts into objects with the index number as property name and the value as property value.
Associative arrays converts into objects with the keys as property names and values as property values.
Example
<pre >
<?php
$a = array ("Volvo" , "BMW" , "Toyota" ); // indexed array
$b = array ("Peter" =>"35" , "Ben" =>"37" , "Joe" =>"43" ); // associative array
$a = (object ) $a;
$b = (object ) $b;
var_dump($a);
var_dump($b);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
7) Cast to NULL
To cast to NULL, use the (unset) statement:
Warning
This feature has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this feature is highly discouraged.
Example
<pre >
<?php
$a = (unset ) 25 ;
$b = (unset ) 25.4 ;
$c = (unset ) "hello" ;
$d = (unset ) true ;
$e = (unset ) NULL ;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Implicit Casting
Implicit casting happens automatically, such as when combining a string with a number.
Example
<pre >
<?php
$number = 10 ; // Integer
$text = "The number is " . $number; // PHP automatically casts $number to a string
echo $text."<br>" ; // Outputs: "The number is 10"
var_dump ($text)
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself