What are the conditions in an if statement?

Well, it is clear that there can be conditions like a > b, a >= b, a + b = c, and so on, but what else can be written? For example, I need to write – if the address of the page on which this code is located is such-and-such, then output such-and-such.

Answer 1, authority 100%

In the if() condition, you can write any mathematical expressions, not only conditional ones.

if ($a % $b)
if ($a = $b+$c)
if ($a++)
if (++$a)
if ($a >> $b)
if ($a === $b)
if ($a !== $b)
if ($res = mysql_fetch($sqlRes))

Document by if:

The if condition works like this: it tests the result of the expression inside the parentheses to see if it’s TRUE or FALSE. If the result is of a type other than Boolean, then the conversion to Boolean takes place first.

Dock on casting to boolean type:
http://www.php.net/manual/en/ language.types.boolean.php#language.types.boolean.casting

When converting to boolean, the following values ​​are treated as FALSE:

  • The boolean itself is FALSE
  • integer 0 (zero)
  • float 0.0 (zero)
  • an empty string and the string “0”
  • empty array
  • object without attributes (PHP4 only)
  • special type NULL (including unset variables)

All other values ​​are treated as TRUE (including any resource).

Answer 2, authority 75%


Answer 3, authority 25%

if ($_SERVER['REQUEST_URI'] == "/somewhere/") {
print "selected";


print ($_SERVER['REQUEST_URI'] == "/somewhere/") ? 'selected' : '';