+ 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
5 ответов
+ 6
You are welcome Jorge! happy coding 👍
+ 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 ...
+ 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
+ 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
+ 2
No problem :)