+ 1

can somebody explain this code to me?

function func($arg) { $result = 0; for($i=0; $i<$arg; $i++) { $result = $result + $i; } return $result; } echo func(5);

29th Sep 2018, 4:57 PM
Niranjan Kumar
Niranjan Kumar - avatar
2 Réponses
30th Sep 2018, 10:00 AM
¥^$#°√€
¥^$#°√€ - avatar
+ 2
This is a function will execute for 5 times as you see on the statement echo func(5). Hence $i the first time is 0, $i is minor to $arg, you have to cout forward. Return to this operation 5 times($result=$result+$i) $i for the 2th time become 1, 3th=2, 4th=3, 5th=4 add to the $result, 1th=0, 2th=1, 3th=3, 4th=6 then $i for fitfh time become 4 add to the forth $result= 6.ok? 5th time the $result is 10. I hope as this may help you
29th Sep 2018, 11:41 PM
¥^$#°√€
¥^$#°√€ - avatar