adding variable numbers in PHP loop counter -
i've got php loop , counter called $total set 0 before loop. once loop starts adding value of $weight loop counter. did this:
$total=0; foreach ($weights $weight){ $total = $total+$weight; } but realised works this:
$total=0; foreach ($weights $weight){ $total += $weight; } question correct method or if both correct better method?
thanks
both correct, , neither better, strictly speaking. they function identically.
that said, coding styles prefer += because it's easier read, while others prefer $x = $x + $y because requires more deliberate action write, makes more obvious happening, , reduces likelihood of single-character typo.
Comments
Post a Comment