php 简单函数中未定义的偏移量()
我不确定为什么我会收到关于此的未定义偏移通知:
<?php
$numbers = array('1','2','3');
$total = 0;
for($i=0;$i<=sizeof($numbers); $i++) {
$total += $numbers[$i];
echo $total;
}
?>
输出:
136 注意:未定义的偏移量:3 位于 E:\php\arrays\array_1.php 第 17 行 6
I'm not sure why I am getting an Undefined Offset Notice on this:
<?php
$numbers = array('1','2','3');
$total = 0;
for($i=0;$i<=sizeof($numbers); $i++) {
$total += $numbers[$i];
echo $total;
}
?>
Output:
136
Notice: Undefined offset: 3 in E:\php\arrays\array_1.php on line 17
6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的数组在索引 0、1 和 2 处有三个元素。没有索引为 3 的元素。
您的循环应该在遇到该元素之前停止...
另外,请检查 array_sum,这可能就是你想要的......
Your array has three elements at index 0, 1 and 2. There is no element with index 3.
Your loop should stop before it hits that...
Also, checkout array_sum, which might be what you're wanting anyway...
您应该循环到
<
数组的大小,而不是<=
。You should loop to
<
the size of the array, not<=
.将条件从
<=
更改为<
。这将正确添加:
Change your condition from
<=
to<
.This will add properly:
关闭 html 错误
turnoff html errors