0
Pregunta
function func($arg) { $result=0 for ($i=0; $i <$arg; $i++) { $result=$result+2; } return $result; } echo func(5); Porque todo esto da un resultado de 10 si result=0 y solo le sumo 2
1 Réponse
+ 1
because the loop goes for 5 times and you add to result its previous value's
i = 0 -> result = 0 + 2;
adding 1 to i
i = 1 -> result = 2 + 2;
adding 1 to i
i = 2 -> result = 4 + 2;
adding 1 to i
i = 3 -> result = 6 + 2;
adding 1 to i
i = 4 -> result = 8 + 2;
adding 1 to i
i = 5
now i is not < 5 so
return result that is 10