PHP: The big php variable type casting!

php-logoI wrote a small script that does every available cast in PHP on every datattype.
Check it out on GistHub:
https://gist.github.com/1341572

The output of the script should be the following:

$ php casting.php

--- Casting all in PHP available Types in every way ---

Info:
In PHP we have the following data types: boolean, int, float, double, array, object,
while float and double should be the same.
There are other types that can not be created by cast, like resources.
PHP is weak typed and allows to cast everything to everything.
I think this is something every (good) Software Programmer working with PHP should know.
Check out the results of the casts, especially to boolean, so you do not need
if(strlen($s) > 0 || count($a) > 0) ... anymore.

PHP-Version: 5.3.5-1ubuntu7.3

--- casting 'null' ------------
(boolean)null -> (boolean)false
(int)null -> (integer)0
(float)null -> (double)0
(double)null -> (double)0
(string)null -> (string)''
(array)null -> (array)array ()
(object)null -> (object)stdClass

--- casting '0' ------------
(boolean)0 -> (boolean)false
(int)0 -> (integer)0
(float)0 -> (double)0
(double)0 -> (double)0
(string)0 -> (string)'0'
(array)0 -> (array)array (0 => 0,)
(object)0 -> (object)stdClass

--- casting '1' ------------
(boolean)1 -> (boolean)true
(int)1 -> (integer)1
(float)1 -> (double)1
(double)1 -> (double)1
(string)1 -> (string)'1'
(array)1 -> (array)array (0 => 1,)
(object)1 -> (object)stdClass

--- casting '0.0' ------------
(boolean)0.0 -> (boolean)false
(int)0.0 -> (integer)0
(float)0.0 -> (double)0
(double)0.0 -> (double)0
(string)0.0 -> (string)'0'
(array)0.0 -> (array)array (0 => 0,)
(object)0.0 -> (object)stdClass

--- casting '0.1' ------------
(boolean)0.1 -> (boolean)true
(int)0.1 -> (integer)0
(float)0.1 -> (double)0.1
(double)0.1 -> (double)0.1
(string)0.1 -> (string)'0.1'
(array)0.1 -> (array)array (0 => 0.1,)
(object)0.1 -> (object)stdClass

--- casting '1.23' ------------
(boolean)1.23 -> (boolean)true
(int)1.23 -> (integer)1
(float)1.23 -> (double)1.23
(double)1.23 -> (double)1.23
(string)1.23 -> (string)'1.23'
(array)1.23 -> (array)array (0 => 1.23,)
(object)1.23 -> (object)stdClass

--- casting 'true' ------------
(boolean)true -> (boolean)true
(int)true -> (integer)1
(float)true -> (double)1
(double)true -> (double)1
(string)true -> (string)'1'
(array)true -> (array)array (0 => true,)
(object)true -> (object)stdClass

--- casting 'false' ------------
(boolean)false -> (boolean)false
(int)false -> (integer)0
(float)false -> (double)0
(double)false -> (double)0
(string)false -> (string)''
(array)false -> (array)array (0 => false,)
(object)false -> (object)stdClass

--- casting '""' ------------
(boolean)"" -> (boolean)false
(int)"" -> (integer)0
(float)"" -> (double)0
(double)"" -> (double)0
(string)"" -> (string)''
(array)"" -> (array)array (0 => '',)
(object)"" -> (object)stdClass

--- casting '"abc"' ------------
(boolean)"abc" -> (boolean)true
(int)"abc" -> (integer)0
(float)"abc" -> (double)0
(double)"abc" -> (double)0
(string)"abc" -> (string)'abc'
(array)"abc" -> (array)array (0 => 'abc',)
(object)"abc" -> (object)stdClass

--- casting 'array()' ------------
(boolean)array() -> (boolean)false
(int)array() -> (integer)0
(float)array() -> (double)0
(double)array() -> (double)0
(string)array() -> (string)'Array'
(array)array() -> (array)array ()
(object)array() -> (object)stdClass

--- casting 'array(1)' ------------
(boolean)array(1) -> (boolean)true
(int)array(1) -> (integer)1
(float)array(1) -> (double)1
(double)array(1) -> (double)1
(string)array(1) -> (string)'Array'
(array)array(1) -> (array)array (0 => 1,)
(object)array(1) -> (object)stdClass

--- casting 'array(1, 2)' ------------
(boolean)array(1, 2) -> (boolean)true
(int)array(1, 2) -> (integer)1
(float)array(1, 2) -> (double)1
(double)array(1, 2) -> (double)1
(string)array(1, 2) -> (string)'Array'
(array)array(1, 2) -> (array)array (0 => 1,1 => 2,)
(object)array(1, 2) -> (object)stdClass

--- casting 'fopen("php://stdout", "w")' ------------
(boolean)fopen("php://stdout", "w") -> (boolean)true
(int)fopen("php://stdout", "w") -> (integer)6
(float)fopen("php://stdout", "w") -> (double)7
(double)fopen("php://stdout", "w") -> (double)8
(string)fopen("php://stdout", "w") -> (string)'Resource id #9'
(array)fopen("php://stdout", "w") -> (array)array (0 => NULL,)
(object)fopen("php://stdout", "w") -> (object)stdClass

--- casting 'new A()' ------------
(boolean)new A() -> (boolean)true
PHP Notice: Object of class A could not be converted to int in casting.php(67) : eval()'d code on line 1
(int)new A() -> (integer)1
PHP Notice: Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1
(float)new A() -> (double)1
PHP Notice: Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1
(double)new A() -> (double)1
(string)new A() -> (string)'A'
(array)new A() -> (array)array ()
(object)new A() -> (object)A

--- PROBLEMS ------------
Some constructs in PHP need a parameter type to work correnctly,
like foreach() and many functions like implode() ....

foreach(array("works") as $a) {}
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 78
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 79
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 80
implode('', array("works"));
PHP Warning: implode(): Invalid arguments passed in casting.php on line 84
PHP Warning: implode(): Invalid arguments passed in casting.php on line 85
PHP Warning: implode(): Invalid arguments passed in casting.php on line 86

Related posts:


 
 
 

Die Kommentarfunktion zu diesem Beitrag wurde deaktiviert.