For example if I want to color every even(every number that divided with 2 equals mod 0) number red in a sequence of 20. I think it should be something like this, but the coloring doesn't work: <html> <body> <? for ($a=1; $a<=20; $a++) if ($a % 2=="0") $color="red"; { echo $a; echo "<br>"; } echo "<font color=\"$color\">\n"; ?> </body> </html>
How can a division ever result in 0? And =="0" means you match it against a string not an integer... See http://www.thephpwtf.com/node/47 for inspiration.
for ( $i=0; $i<=20; $i++) { $color = ($i%2) ? "red" : "black"; echo "<font color=\"$color\">$i is $color</font><br />\n"; } PHP:
Your syntax is wrong for ($a=1; $a<=20; $a++) {if ($a % 2=="0") { $color="red"; echo $a; echo "<br>"; } else { optional statment;} } PHP: you are missing the extra " { } "s that makes it work properly, also a "{" must follow the ")"
for ($a=1; $a<=20; $a++) { if ($a % 2=="0") { $color="red"; echo $a; echo "<br>"; } else { optional statment; } } PHP: I see no problems with the syntax. The problem is when he compares the 0 to "0", php may not type cast the var properly for the test. === may not help either since they are going to cast differently. The solution us use the 0 as the comparator. The code is laid out and the braces balance fine.