AnDeriensのブログ

個人的なブログです

PHPでキャストした時の挙動

PHP書いていて、整数キャストした時にちゃんとバグなく動くのか気になったのでちょっと調べてみた。

調べた時のコードは以下。

GitHub - AnDeriens/type_researcher at v.0.1

int

value result
NULL int(0)
0 int(0)
1 int(1)
'' int(0)
'0' int(0)
'1' int(1)
'a' int(0)
true int(1)
false int(0)
0.1 int(0)
array () int(0)
array ( 0 => 'a',) int(1)
array ( 0 => 'a', 1 => 'b',) int(1)
array ( 0 => 1, 1 => 2,) int(1)
(object) array() E_WARNING
(object) array( 'a' => 'a',) E_WARNING
Closure::__set_state(array()) E_WARNING

string

※ FatalErrorのときは、エラーメッセージを表示しています。

value result
NULL string(0) ""
0 string(1) "0"
1 string(1) "1"
'' string(0) ""
'0' string(1) "0"
'1' string(1) "1"
'a' string(1) "a"
true string(1) "1"
false string(0) ""
0.1 string(3) "0.1"
array () E_WARNING
array ( 0 => 'a',) E_WARNING
array ( 0 => 'a', 1 => 'b',) E_WARNING
array ( 0 => 1, 1 => 2,) E_WARNING
(object) array() string(70) "[Fatal Error]Object of class stdClass could not be converted to string"
(object) array( 'a' => 'a',) string(70) "[Fatal Error]Object of class stdClass could not be converted to string"
Closure::__set_state(array()) string(70) "[Fatal Error]Object of class Closure could not be converted to string "

boolean

value result
NULL bool(false)
0 bool(false)
1 bool(true)
'' bool(false)
'0' bool(false)
'1' bool(true)
'a' bool(true)
true bool(true)
false bool(false)
0.1 bool(true)
array () bool(false)
array ( 0 => 'a',) bool(true)
array ( 0 => 'a', 1 => 'b',) bool(true)
array ( 0 => 1, 1 => 2,) bool(true)
(object) array() bool(true)
(object) array( 'a' => 'a',) bool(true)
Closure::__set_state(array()) bool(true)

float

value result
NULL float(0)
0 float(0)
1 float(1)
'' float(0)
'0' float(0)
'1' float(1)
'a' float(0)
true float(1)
false float(0)
0.1 float(0.1)
array () float(0)
array ( 0 => 'a',) float(1)
array ( 0 => 'a', 1 => 'b',) float(1)
array ( 0 => 1, 1 => 2,) float(1)
(object) array() E_WARNING
(object) array( 'a' => 'a',) E_WARNING
Closure::__set_state(array()) E_WARNING

array

value result
NULL array(0) {}
0 array(1) { [0]=> int(0)}
1 array(1) { [0]=> int(1)}
'' array(1) { [0]=> string(0) ""}
'0' array(1) { [0]=> string(1) "0"}
'1' array(1) { [0]=> string(1) "1"}
'a' array(1) { [0]=> string(1) "a"}
true array(1) { [0]=> bool(true)}
false array(1) { [0]=> bool(false)}
0.1 array(1) { [0]=> float(0.1)}
array () array(0) {}
array ( 0 => 'a',) array(1) { [0]=> string(1) "a"}
array ( 0 => 'a', 1 => 'b',) array(2) { [0]=> string(1) "a" [1]=> string(1) "b"}
array ( 0 => 1, 1 => 2,) array(2) { [0]=> int(1) [1]=> int(2)}
(object) array() array(0) {}
(object) array( 'a' => 'a',) array(1) { ["a"]=> string(1) "a"}
Closure::__set_state(array()) array(1) { [0]=> object(Closure)#6 (0) { }}

object

value result
NULL object(stdClass)#4 (0) {}
0 object(stdClass)#4 (1) { ["scalar"]=> int(0)}
1 object(stdClass)#4 (1) { ["scalar"]=> int(1)}
'' object(stdClass)#4 (1) { ["scalar"]=> string(0) ""}
'0' object(stdClass)#4 (1) { ["scalar"]=> string(1) "0"}
'1' object(stdClass)#4 (1) { ["scalar"]=> string(1) "1"}
'a' object(stdClass)#4 (1) { ["scalar"]=> string(1) "a"}
true object(stdClass)#4 (1) { ["scalar"]=> bool(true)}
false object(stdClass)#4 (1) { ["scalar"]=> bool(false)}
0.1 object(stdClass)#4 (1) { ["scalar"]=> float(0.1)}
array () object(stdClass)#4 (0) {}
array ( 0 => 'a',) object(stdClass)#4 (1) { ["0"]=> string(1) "a"}
array ( 0 => 'a', 1 => 'b',) object(stdClass)#4 (2) { ["0"]=> string(1) "a" ["1"]=> string(1) "b"}
array ( 0 => 1, 1 => 2,) object(stdClass)#4 (2) { ["0"]=> int(1) ["1"]=> int(2)}
(object) array() object(stdClass)#4 (0) {}
(object) array( 'a' => 'a',) object(stdClass)#4 (1) { ["a"]=> string(1) "a"}
Closure::__set_state(array()) object(Closure)#6 (0) {}