PHP
<?php
$cars = array("a","b","c");
var_dump($cars);
$cars[4] = "d";
var_dump($cars);
$len = count($cars);
for($x = 0; $x < $len; $x++){
echo "<br>";
echo $cars[$x];
echo "<br>";
}
?>
JavaScript
但是发现js中好像没有这种问题
var arr = ["a","b","c"];
console.log(arr);
arr[4] = "d";
console.log(arr);
console.log(arr[4]);
for(let i = 0; i < arr.length;i++){
console.log(arr[i]);
}
$ node index.js
[ 'a', 'b', 'c' ]
[ 'a', 'b', 'c', <1 empty item>, 'd' ]
d
a
b
c
undefined
d
js中,遇到不存在的索引值arr[3]
时,会打印undefined,但是还是会将后边的值打印出来,但是PHP中好像并不会把后面的值打印出来。