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

    3 answers 3

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


     print ($_SERVER['REQUEST_URI'] == "/somewhere/") ? 'selected' : ''; 
    • it is better to include a file by the link name than to write a condition (or switch) on each page. - Pavel Vladimirov

    In the condition if () 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)) 

    Dock on if: http://www.php.net/manual/en/control-structures.if.php

    The if condition works like this: it checks the result of the expression inside the parentheses for a TRUE or FALSE equality. If the result is of a type other than Boolean, then the conversion to Boolean occurs first.

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

    When converting to a boolean type, the following values ​​are considered FALSE:

    • Boolean itself FALSE
    • integer 0 (zero)
    • floating point number 0.0 (zero)
    • empty line and 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).