PHP has three primary conditional statements: if, else and else if (which can also be written elseif).
Conditional statements are used to perform different actions based on different conditions.
1) Every conditional begins with an "if()" clause. This conditional executes some code only if a specified condition is TRUE.
if (condition) { // code to be executed if condition is TRUE }
- Example:
<?php $nr = 8; if (isset($nr)) { echo 'Nr. is: '. $nr; } ?>This code will output: "Nr. is: 8".
The PHP isset() function is very often used with the "if" clause. This function checks if a variable is set (with a value other than NULL), returns TRUE if the variable is defined, otherwise, FALSE.
if (condition) { // code to be executed if condition is TRUE }
else { // code to be executed if condition is FALSE }
- Example:
<?php if (isset($nr)) { echo 'Nr. is: '. $nr; } else { echo 'The $nr variable is not defined'; } ?>Becouse the $nr variable isn't set, the "isset($nr)" condition returns FALSE, this makes to be executed the code from "else" statement, and will output: "The $nr variable is not defined".
if (condition1) { // code to be executed if condition1 is TRUE } elseif (condition2) { // code to be executed if condition1 is FALSE and condition2 is TRUE } else { // code to be executed if all conditions are FALSE }You can use as many "elseif" clauses as you want (after the "if()"). If a condition is TRUE, the code in the following curly braces ({}) will be executed. If not, PHP will continue on. If there is a second condition (after an elseif), that will be checked for truth. The process will continue until PHP hits an "else", which will be automatically executed at that point, or until the conditional statement terminates without an "else". It's important that the "else" always come last and be treated as the default action.
<?php $nr = NULL; if (isset($nr)) { echo 'Nr. is: '. $nr; } elseif(is_null($nr)) { echo 'Nr is NULL'; } else { echo 'The $nr variable is not defined'; } ?>isset($nr) returns TRUE only if $nr is set and with a value other than NULL. So, in the above code this condition returns FALSE, and PHP checks the next condition (of the "elseif" clause).
The PHP is_null($var) function finds whether the given variable is NULL. Returns TRUE if $var is null, FALSE otherwise.
Operator | Description | Example |
---|---|---|
== | is equal to | 8==8 returns true |
=== | is identical to | 0===false returns false |
!= | is not equal | 7!=8 returns true |
<> | is not equal | 7<>8 (true) |
> | is greater than | 7>8 (false) |
< | is less than | 7<8 (true) |
>= | is greater than or equal to | 8>=8 (true) |
<= | is less than or equal to | 7<=8 (true) |
&& | and | $x=8; $y=10; ($x<5 && $y>7) // false |
|| | or | $x=8; $y=10; ($x<5 || $y>7) // true |
! | not | $x=8; $y=10; !($x==$y) // true |
<?php $name = 'MarPlo'; if($name=='MarPlo') { echo 'Hy '. $name; } else { echo 'Hy there.'; } ?>
<?php $x = 8; $y = 32; if ($x<11 && $y>32) { echo 'x<11 and y>32'; } elseif ($x<11 || $y>32) { echo 'x<11 or y>32'; } else { echo $x. ' - '. $y; } ?>
<?php $num = 88; if ($num > 10) { if ($num > 100) echo "It is a number greater than 100"; else echo "It is a number less than 100 but greater than 10"; } else echo "This is a small number"; ?>
(condition) ? result_for_TRUE : result_for_FALSE;
- Example:
<?php $age = 12; // ternary statement $agestr = ($age < 18) ? 'child' : 'adult'; echo $agestr; // child ?>- In the ternary statement, first there is a condition ($age < 18), then there is a question mark (?), then a true-result, a colon (:), and a false-result. If $age is less than 18, $agestr will be set to 'child', otherwise it will be set to 'adult'.
<?php $age = 12; if ($age < 18) { $agestr = 'child'; } else { $agestr = 'adult'; } echo $agestr; ?>
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);