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
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...
for($i=0;$i<sizeof($numbers); $i++) {
}
Also, checkout array_sum, which might be what you're wanting anyway...
$total=array_sum($numbers);
You should loop to <
the size of the array, not <=
.
for($i=0;$i<sizeof($numbers); $i++) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With