169
правок
AE (обсуждение | вклад) (Приведение типов) |
AE (обсуждение | вклад) |
||
Строка 104: | Строка 104: | ||
== Приведение типов == | == Приведение типов == | ||
{| class="wikitable" | |||
|+ Примеры приведения типов | |||
! $v = ... | |||
! gettype($v) | |||
! (bool)$v | |||
! (int)$v | |||
! (float)$v | |||
! (string)$v | |||
! (array)$v | |||
! (object)$v | |||
|- | |||
! null | |||
| string(4) "NULL" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| string(0) "" | |||
| [] | |||
| {} | |||
|- | |||
! true | |||
| string(7) "boolean" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| string(1) "1" | |||
| [true] | |||
| {"scalar":true} | |||
|- | |||
! false | |||
| string(7) "boolean" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| string(0) "" | |||
| [false] | |||
| {"scalar":false} | |||
|- | |||
! 0 | |||
| string(7) "integer" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| string(1) "0" | |||
| [0] | |||
| {"scalar":0} | |||
|- | |||
! 1 | |||
| string(7) "integer" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| string(1) "1" | |||
| [1] | |||
| {"scalar":1} | |||
|- | |||
! 1.0 | |||
| string(6) "double" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| string(1) "1" | |||
| [1] | |||
| {"scalar":1} | |||
|- | |||
! M_PI | |||
| string(6) "double" | |||
| bool(true) | |||
| int(3) | |||
| float(3.141592653589793) | |||
| string(15) "3.1415926535898" | |||
| [3.141592653589793] | |||
| {"scalar":3.141592653589793} | |||
|- | |||
! INF | |||
| string(6) "double" | |||
| bool(true) | |||
| int(0) | |||
| float(INF) | |||
| string(3) "INF" | |||
| array(1) { | |||
[0]=> | |||
float(INF) | |||
} | |||
| object(stdClass)#5 (1) { | |||
["scalar"]=> | |||
float(INF) | |||
} | |||
|- | |||
! NAN | |||
| string(6) "double" | |||
| bool(true) | |||
| int(0) | |||
| float(NAN) | |||
| string(3) "NAN" | |||
| array(1) { | |||
[0]=> | |||
float(NAN) | |||
} | |||
| object(stdClass)#5 (1) { | |||
["scalar"]=> | |||
float(NAN) | |||
} | |||
|- | |||
! "" | |||
| string(6) "string" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| string(0) "" | |||
| [""] | |||
| {"scalar":""} | |||
|- | |||
! "0" | |||
| string(6) "string" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| string(1) "0" | |||
| ["0"] | |||
| {"scalar":"0"} | |||
|- | |||
! "1" | |||
| string(6) "string" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| string(1) "1" | |||
| ["1"] | |||
| {"scalar":"1"} | |||
|- | |||
! "true" | |||
| string(6) "string" | |||
| bool(true) | |||
| int(0) | |||
| float(0) | |||
| string(4) "true" | |||
| ["true"] | |||
| {"scalar":"true"} | |||
|- | |||
! "false" | |||
| string(6) "string" | |||
| bool(true) | |||
| int(0) | |||
| float(0) | |||
| string(5) "false" | |||
| ["false"] | |||
| {"scalar":"false"} | |||
|- | |||
! "null" | |||
| string(6) "string" | |||
| bool(true) | |||
| int(0) | |||
| float(0) | |||
| string(4) "null" | |||
| ["null"] | |||
| {"scalar":"null"} | |||
|- | |||
! [] | |||
| string(5) "array" | |||
| bool(false) | |||
| int(0) | |||
| float(0) | |||
| | |||
Warning: Array to string conversion in /home/user/scripts/code.php on line 125 | |||
string(5) "Array" | |||
| [] | |||
| {} | |||
|- | |||
! [0] | |||
| string(5) "array" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| | |||
Warning: Array to string conversion in /home/user/scripts/code.php on line 125 | |||
string(5) "Array" | |||
| [0] | |||
| {"0":0} | |||
|- | |||
! ["abc"] | |||
| string(5) "array" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| | |||
Warning: Array to string conversion in /home/user/scripts/code.php on line 125 | |||
string(5) "Array" | |||
| ["abc"] | |||
| {"0":"abc"} | |||
|- | |||
! ["a"=>"b"] | |||
| string(5) "array" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| | |||
Warning: Array to string conversion in /home/user/scripts/code.php on line 125 | |||
string(5) "Array" | |||
| {"a":"b"} | |||
| {"a":"b"} | |||
|- | |||
! [["a"=>"b"]] | |||
| string(5) "array" | |||
| bool(true) | |||
| int(1) | |||
| float(1) | |||
| | |||
Warning: Array to string conversion in /home/user/scripts/code.php on line 125 | |||
string(5) "Array" | |||
| [{"a":"b"}] | |||
| {"0":{"a":"b"}} | |||
|- | |||
! new stdClass() | |||
| string(6) "object" | |||
| bool(true) | |||
| | |||
Warning: Object of class stdClass could not be converted to int in /home/user/scripts/code.php on line 125 | |||
int(1) | |||
| | |||
Warning: Object of class stdClass could not be converted to float in /home/user/scripts/code.php on line 125 | |||
float(1) | |||
| Object of class stdClass could not be converted to string | |||
| [] | |||
| {} | |||
|- | |||
! new class {function __toString(){ return "value"; }} | |||
| string(6) "object" | |||
| bool(true) | |||
| | |||
Warning: Object of class class@anonymous could not be converted to int in /home/user/scripts/code.php on line 125 | |||
int(1) | |||
| | |||
Warning: Object of class class@anonymous could not be converted to float in /home/user/scripts/code.php on line 125 | |||
float(1) | |||
| string(5) "value" | |||
| [] | |||
| {} | |||
|- | |||
! (function() { yield 1; yield 2; })() | |||
| string(6) "object" | |||
| bool(true) | |||
| | |||
Warning: Object of class Generator could not be converted to int in /home/user/scripts/code.php on line 125 | |||
int(1) | |||
| | |||
Warning: Object of class Generator could not be converted to float in /home/user/scripts/code.php on line 125 | |||
float(1) | |||
| Object of class Generator could not be converted to string | |||
| [] | |||
| {} | |||
|- | |||
! fopen('file', 'r') | |||
| string(8) "resource" | |||
| bool(true) | |||
| int(6) | |||
| float(6) | |||
| string(14) "Resource id #6" | |||
| array(1) { | |||
[0]=> | |||
resource(6) of type (stream) | |||
} | |||
| object(stdClass)#5 (1) { | |||
["scalar"]=> | |||
resource(6) of type (stream) | |||
} | |||
|- | |||
|colspan="25"| * ремарка | |||
|- | |||
|} | |||
== Объявление типов для строгой типизации == | == Объявление типов для строгой типизации == |