php Undefined Offset in simple function()

Old*_*est 4 php arrays for-loop sizeof

我不知道为什么我会收到一份未定义的抵消通知:

<?php 

$numbers = array('1','2','3');
$total = 0;

for($i=0;$i<=sizeof($numbers); $i++) {
    $total += $numbers[$i];
    echo $total;
}

?>
Run Code Online (Sandbox Code Playgroud)

输出:

136注意:未定义的偏移量:在第17行的 E:\ php\arrays\array_1.php中为3 6

Pau*_*xon 7

您的数组在索引0,1和2处有三个元素.没有索引为3的元素.

你的循环应该在它到达之前停止......

for($i=0;$i<sizeof($numbers); $i++) {
}
Run Code Online (Sandbox Code Playgroud)

另外,checkout array_sum,这可能是你想要的......

$total=array_sum($numbers);
Run Code Online (Sandbox Code Playgroud)