meta property="og:image" content="https://codelines.in/wp-content/uploads/2024/02/php_logo-1.png">
HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP Casting


PHP Object manipulation in PHP: Converting objects to other classes


PHP casting is the process of explicitly converting a value from one data type to another. This can be useful for various reasons, such as:

Ensuring compatibility between data types in operations or function calls.

Formatting output for specific purposes.

Working with data retrieved from other sources that might have different types.


Change Data Type


Casting in PHP is done with these statements:

• (string) - Converts to data type String
• (int) - Converts to data type Integer
• (float) - Converts to data type Float
• (bool) - Converts to data type Boolean
• (array) - Converts to data type Array
• (object)- Converts to data type Object
• (unset) - Converts to data type NULL


Cast to String


To cast to string, use the (string) statement:

Example


<!DOCTYPE html>
<html>
<body>

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


<p>Note that when casting a Boolean into string it gets the value "1", and when casting NULL into string it is converted into an empty string "".</p>

</body>
</html>

Output

string(1) "5"
string(4) "5.34"
string(5) "hello"
string(1) "1"
string(0) ""

Note that when casting a Boolean into string it gets the value "1", and when casting NULL into string it is converted into an empty string "".

Cast to Integer


To cast to integer, use the (int) statement:

Example


<!DOCTYPE html>
<html>
<body>

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

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

</body>
</html>

Output

int(5)
int(5)
int(25)
int(0)
int(0)
int(1)
int(0)

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

Cast to Float


To cast to float, use the (float) statement:

Example


<!DOCTYPE html>
<html>
<body>

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

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

Output

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

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.

Cast to Boolean


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

Example


<!DOCTYPE html>
<html>
<body>

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

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

Output

bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)

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

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


Cast to Array


To cast to array, use the (array) statement:

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>

<p>When converting into arrays, most data types converts into an indexed array with one element.</p>

<p>NULL values converts to an empty array object.</p>

</body>
</html>
     

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) { }

When converting into arrays, most data types converts into an indexed array with one element.
NULL values converts to an empty array object.

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

Cast to Object


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

Example


<!DOCTYPE html>
<html>
<body>

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

<p>When converting into objects, most data types converts into a object with one property, named "scalar", with the corresponding value.</p>
<p>NULL values converts to an empty object.</p>

</body>
</html>
     

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) {
}

When converting into objects, most data types converts into a object with one property, named "scalar", with the corresponding value.
NULL values converts to an empty object.

When converting into objects, most data types converts into a object with one property, named "scalar", with the corresponding value.

NULL values converts to an empty object.

Indexed arrays converts into objects with the index number as property name and the value as property value.



Cast to NULL


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

Example


<!DOCTYPE html>
<html>
<body>

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

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

Output

NULL
NULL
NULL
NULL
NULL