arrays - php Undefined Offset in simple function() -



arrays - php Undefined Offset in simple function() -

i'm not sure why getting 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 3 elements @ index 0, 1 , 2. there no element index 3.

your loop should stop before hits that...

for($i=0;$i<sizeof($numbers); $i++) { }

also, checkout array_sum, might you're wanting anyway...

$total=array_sum($numbers);

php arrays for-loop sizeof

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -