Created
November 2, 2014 14:16
-
-
Save paranoiq/89d532be7dc690885666 to your computer and use it in GitHub Desktop.
casting in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Abc { | |
public $a = 1; | |
protected $b = 2; | |
private $c = 3; | |
} | |
$resource = tmpfile(); | |
echo "\nbool:\n"; | |
var_dump((bool) null); | |
var_dump((bool) false); | |
var_dump((bool) true); | |
var_dump((bool) 123); | |
var_dump((bool) 123.456); | |
var_dump((bool) ''); | |
var_dump((bool) '123'); | |
var_dump((bool) '123abc'); | |
var_dump((bool) 'abc123'); | |
var_dump((bool) array()); | |
var_dump((bool) array(null)); | |
var_dump((bool) array(123)); | |
var_dump((bool) array(1, 2, 3)); | |
var_dump((bool) new StdClass()); | |
var_dump((bool) new Abc()); | |
var_dump((bool) $resource); | |
echo "\nint:\n"; | |
var_dump((int) null); | |
var_dump((int) false); | |
var_dump((int) true); | |
var_dump((int) 123); | |
var_dump((int) 123.456); | |
var_dump((int) ''); | |
var_dump((int) '123'); | |
var_dump((int) '123abc'); | |
var_dump((int) 'abc123'); | |
var_dump((int) array()); | |
var_dump((int) array(null)); | |
var_dump((int) array(123)); | |
var_dump((int) array(1, 2, 3)); | |
var_dump((int) new StdClass()); | |
var_dump((int) new Abc()); | |
var_dump((int) $resource); | |
echo "\nfloat:\n"; | |
var_dump((float) null); | |
var_dump((float) false); | |
var_dump((float) true); | |
var_dump((float) 123); | |
var_dump((float) 123.456); | |
var_dump((float) ''); | |
var_dump((float) '123'); | |
var_dump((float) '123abc'); | |
var_dump((float) 'abc123'); | |
var_dump((float) array()); | |
var_dump((float) array(null)); | |
var_dump((float) array(123)); | |
var_dump((float) array(1, 2, 3)); | |
var_dump((float) new StdClass()); | |
var_dump((float) new Abc()); | |
var_dump((float) $resource); | |
echo "\nstring:\n"; | |
var_dump((string) null); | |
var_dump((string) false); | |
var_dump((string) true); | |
var_dump((string) 123); | |
var_dump((string) 123.456); | |
var_dump((string) ''); | |
var_dump((string) '123'); | |
var_dump((string) '123abc'); | |
var_dump((string) 'abc123'); | |
var_dump((string) array()); | |
var_dump((string) array(null)); | |
var_dump((string) array(123)); | |
var_dump((string) array(1, 2, 3)); | |
//var_dump((string) new StdClass()); // fatal | |
//var_dump((string) new Abc()); // fatal | |
var_dump((string) $resource); | |
echo "\narray:\n"; | |
var_dump((array) null); | |
var_dump((array) false); | |
var_dump((array) true); | |
var_dump((array) 123); | |
var_dump((array) 123.456); | |
var_dump((array) ''); | |
var_dump((array) '123'); | |
var_dump((array) '123abc'); | |
var_dump((array) 'abc123'); | |
var_dump((array) array()); | |
var_dump((array) array(null)); | |
var_dump((array) array(123)); | |
var_dump((array) array(1, 2, 3)); | |
var_dump((array) new StdClass()); | |
var_dump((array) new Abc()); | |
var_dump((array) $resource); | |
echo "\nobject:\n"; | |
var_dump((object) null); | |
var_dump((object) false); | |
var_dump((object) true); | |
var_dump((object) 123); | |
var_dump((object) 123.456); | |
var_dump((object) ''); | |
var_dump((object) '123'); | |
var_dump((object) '123abc'); | |
var_dump((object) 'abc123'); | |
var_dump((object) array()); | |
var_dump((object) array(null)); | |
var_dump((object) array(123)); | |
var_dump((object) array(1, 2, 3)); | |
var_dump((object) new StdClass()); | |
var_dump((object) new Abc()); | |
var_dump((object) $resource); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment