hmm interesting, because executing the below code is valid. Observe:
for ($i = 1; $i <=10; $i++) {
if ($i % 2) {
echo $i;
}
}
The above code indeed, will output 13579
Notice no 'else' clause was used in the above.
If you wanted to inform the user of whether $i % 2 == FALSE ($i's divisor yielded remainder 0), you could include an else clause to print out the even numbers like shown below:
for ($i = 1; $i <=10; $i++) {
if ($i % 2) {
echo "$i is odd";
echo "<br />";
} else {
echo "$i is even";
echo "<br />";
}
}
Giving you the output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
I hope my amazingly easy to understand examples will help all newcomers to PHP, hands down the 'best' server-side scripting language for building dynamic web applications :-)
if
, more of a shortif/else
(although even that is just a very simplistic way of looking at it) – Punchy[1,null][$thisVar==$thatVar] ?? doThis();
– Oversew[1, null]
is a 2 element array, and[$thisVar==$thatVar]
is an array accessor, which accesses element 0 if its false or element 1 if its true, and element 1 will null coalesce. onecompiler.com/php/3xr67v6w3 – Oversew