可能值得在文档中指出并非所有关联(字符串)键都一样,以下代码的输出证明了这一点——这可能对一些人来说是 PHP 自动类型转换的简明介绍(并避免一些头痛)
<?php
$r = array("0"=>"0","1"=>"1","" =>"2"," "=>"3");
echo 'how php sees this array: array("0"=>"0","1"=>"1","" =>"2"," "=>"3")',"\n-----------\n";
var_dump($r); print_r($r); var_export($r);
echo "\n-----------\n",'var_dump("0","1",""," ") = ',"\n-----------\n";
var_dump("0","1",""," ");
?>
输出
how php sees this array: array("0"=>"0","1"=>"1","" =>"2"," "=>"3")
-----------
array(4) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[""]=>
string(1) "2"
[" "]=>
string(1) "3"
}
Array
(
[0] => 0
[1] => 1
[] => 2
[ ] => 3
)
array (
0 => '0',
1 => '1',
'' => '2',
' ' => '3',
)
-----------
var_dump("0","1",""," ") =
-----------
string(1) "0"
string(1) "1"
string(0) ""
string(1) " "