PHP Casting

Type casting refers to converting a value from one data type to another.


There are two types of casting in PHP:


  1. Explicit casting
  2. Implicit casting (type juggling)


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
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 24; // Integer
$b = 2.3; // Float
$c = "25 apples"; // String
$d = "apples 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);
?>
</pre>


</body>
</html>

Try it yourself

2) Cast to Boolean


To cast to boolean, use the (bool) statement:

Example
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 25; // Integer
$b = 25.4; // 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);
var_dump($h);
var_dump($i);
?>
</pre>

<p>If a value is 0, NULL, false, or empty, the (bool) converts it into false, otherwise true. Even -1 converts to true.</p>

</body>
</html>

Try it yourself

3) Cast to Float


When casting to a float, the value is treated as a decimal number.

Example
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 25; // Integer
$b = 25.4; // Float
$c = "25 apples"; // String
$d = "apples 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 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>

<p>Note that when casting a string that starts with a number, the (float) function uses that number. If it does not start with a number, the (float) function convert strings into the number 0.</p>

</body>
</html>

Try it yourself

4) Cast to String


You can cast integers, floats, or booleans to strings.

Example
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 25; // Integer
$b = 25.4; // 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;

var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>
</pre>


</body>
</html>

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
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$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;

//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>


</body>
</html>

Try it yourself

Objects converts into associative arrays where the property names becomes the keys and the property values becomes the values.

Example
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Try it yourself

6) Type Casting in Objects


To cast to object, use the (object) statement.

Example
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 25; // Integer
$b = 25.4; // 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);
?>
</pre>


</body>
</html>

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
<!DOCTYPE html>
<html>
<body>

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


</body>
</html>

Try it yourself

7) Cast to NULL


To cast to NULL, use the (unset) statement:

Example
<!DOCTYPE html>
<html>
<body>

<pre>
<?php
$a = 25; // Integer
$b = 25.4; // 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);
?>
</pre>

</body>
</html>

Try it yourself

Implicit Casting


Implicit casting happens automatically, such as when combining a string with a number.

Example
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Try it yourself

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.