+ 1

I am new to php can any one explain why the ans is 10?

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

4th Mar 2017, 12:22 PM
SAURAV DEY
SAURAV DEY - avatar
5 Respostas
+ 12
It's not just PHP but it is also the same with JavaScript
4th Mar 2017, 1:06 PM
Siddharth Saraf
+ 10
when i=0 0+0=0, when i=1 0+1=1, ... 1+2=3, ... 3+3=6, ... 6+4=10
4th Mar 2017, 12:36 PM
C.E.
C.E. - avatar
+ 1
thank you Elizabeth & Siddharth
4th Mar 2017, 2:39 PM
SAURAV DEY
SAURAV DEY - avatar
+ 1
Now I expect the answer is 10.
8th May 2017, 5:57 AM
SAURAV DEY
SAURAV DEY - avatar
0
btw s what you expect?
7th May 2017, 8:38 AM
Yogesh Kulkarni
Yogesh Kulkarni - avatar