PHP Casting

Back to ACP page

Table of Contents

PHP Cast to String

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

Example 1

Result View Example

PHP Cast to Integer

<h2>Cast_to_Integer</h2> <p>From w3schools.com, Experiment by Teeratus_R </p> <pre> <?php $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; //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>

Example 2

Result View Example

PHP Cast to Float

<h2>Cast to Float</h2> <p>To cast to float, use the (float) statement: </p> <pre> <?php $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; //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>

Example 3

Result View Example

PHP Cast to Boolean

<h2>Cast_to_Boolean</h2> <p>From w3schools.com, Experiment by Teeratus_R </p> <pre> <?php $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; //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); var_dump($h); var_dump($i); ?> </pre>

Example 4

Result View Example

PHP Cast to Array

<h2>Cast_to_Array</h2> <p>To cast to array, use the (array) statement:</p> <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>

Example 5

Result View Example

PHP Converting Objects into Arrays

<h2>Converting_Objects_into_Arrays</h2> <pre> <?php 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);?> </pre>

Example 6

Result View Example

PHP Cast to Object

<h2>Cast_to_Object</h2> <p>To cast to object, use the (object) statement: </p> <pre> <?php $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; //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>

Example 7

Result View Example

PHP Converting Arrays into Objects

<h2>Converting_Arrays_into_Objects</h2> <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; //To verify the type of any object in PHP, use the var_dump() function: var_dump($a); var_dump($b); ?> </pre>

Example 8

Result View Example

PHP Cast to NULL

<h2>Cast to NULL</h2> <p>To cast to NULL, use the (unset) statement:</p> <pre> <?php $a = 5; // Integer $b = 5.34; // Float $c = "hello"; // String $d = true; // Boolean $e = NULL; // NULL unset($a); unset($b); unset($c); unset($d); unset($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>

Example 9

Result View Example

Document

Document in project

You can Download PDF file.

Reference