+ 2

need php code explanation

please tell me how can the following code output 10?? function func($arg) { $result = 0; for($i=0; $i<$arg; $i++) { $result = $result + $i; } return $result; } echo func(5); please break down this code and explain me in details and may someone else too benefit from this.

9th Mar 2017, 3:06 PM
Hussain Mohammed
Hussain Mohammed - avatar
6 Antworten
+ 2
Here in this code you are defining a function called "func" which takes "$arg" as a parameter. Then you're declaring variable "$result" and initializing with 0. Next the number of times for loop runs is "$arg-1" starting from "0", as the loop stops when the value of "$i" is less than "$arg". In the body of the loop, you are finding the sum of elements (numbers) from "0" to "$arg-1" and saving the result in subsequent turns of loops in "$result" variable. Once your "$i" variable is one less than "$arg" the loop stops and the value of "$result" is printed. Imagine "$arg" value as 3. "$result" value is 0. "$i" value is 0. In for loop... Conditional check - "$i" value is less than "$arg" Ist Iteration $result = 0 + 0; increment "$i" value ; "$i"=1, by i++; Conditional check - "$i" value is less than "$arg" 2nd iteration $result = 0 + 1; $result equals 1 increment "$i" value ; "$i"=2, by i++; Conditional check - "$i" value is less than "$arg" $result = 1 + 2; $result equals 3 increment "$i" value ; "$i"=3, by i++; Conditional check - "$i" value is not less than "$arg" Hence loop breaks.. Echo $result
9th Mar 2017, 3:46 PM
Bhargav
Bhargav - avatar
+ 2
You are correct the for loop stops when $i value is 4. You must see that the loop also performs a step to increase the value of $result as explained. This function returns the value of $result and not $i. $i value is incremented upto 5 but the $result value is increased upto 10 as the $i numbers are adding up. Echo statement prints the value of variable..
10th Mar 2017, 11:33 AM
Bhargav
Bhargav - avatar
+ 1
I found the solution for this ! the confusion was causing because i lacked of knowledge how the $result variable was saving the value.. The cycle have only 4 repeat ( if $i < 5 ) ( 4 ) 0 = 0 + 0; Save and repeat; 0 = 0 + 1; save and repeat; 1 = 1 + 2; save and repeat; 3 = 3 + 3; save and repeat; 6 = 6 + 4; = 10 ! ... cycle must end . This is the right explanation. credit goes to @Lukys Martinec
10th Mar 2017, 11:36 AM
Hussain Mohammed
Hussain Mohammed - avatar
+ 1
In each turn you are increasing value of result by 4. 4*5= 20. 4+4+4+4+4=20. result = 0+4; i=0; result= 4+ 4; I=1; result = 8+ 4;I=2 result= 12+4;I=3; result = 16+ 4;I=4; result=20;
11th Mar 2017, 3:13 AM
Bhargav
Bhargav - avatar
0
I still don't get it needs more explanation. the for loop should run till it reaches $arg value right? and the $arg value is 5 so the for loop should break after it reaches 4 why it goes till 10 ??????????
10th Mar 2017, 10:07 AM
Hussain Mohammed
Hussain Mohammed - avatar
0
Well can anyone explain this too ? $arg = 5; $result = 0; for($i=0; $i<$arg; $i++) { $result = $result + 4 ; } echo $result; how is this outputting 20 ?
10th Mar 2017, 5:56 PM
Hussain Mohammed
Hussain Mohammed - avatar