+ 3

PHP Help with example code (Modulus)

Somebody can help me to understand the logic of "if ($i%2==0) in this code? for ($i=0; $i<10; $i++) { if ($i%2==0) { continue; } echo $i . ' '; } //Output: 1 3 5 7 9 By logic I understand it adds 2 units every cycle, by I dont understand the logic of the process

3rd Feb 2019, 7:58 PM
Jorge Cueva
Jorge Cueva - avatar
5 odpowiedzi
+ 6
You are welcome Jorge! happy coding 👍
4th Feb 2019, 5:53 PM
Ipang
+ 5
If value of $i is fully divisible by two (no remainder from the division) then continue with the next loop iteration, otherwise, print value of variable $i. That's what it means, in short, any even number will be skipped, only odd numbers are printed ...
3rd Feb 2019, 9:21 PM
Ipang
+ 3
The % is what the remainder of a division is. So in your program, if $i was 7, the code would not run because when divided by 2, it has a remainder of 1
3rd Feb 2019, 8:18 PM
Eragon1980
Eragon1980 - avatar
+ 3
Now I understand. When it says continue that does not mean that will print the output, that means that will continue but with the loop and adds one unit to the variable. When does not meet the if condition, it will print the value. There where I was confused. Thanks Eragon and lpang for your help!!!! :D
4th Feb 2019, 5:46 PM
Jorge Cueva
Jorge Cueva - avatar
+ 2
No problem :)
4th Feb 2019, 7:43 PM
Eragon1980
Eragon1980 - avatar