If conditionals, Comparative and Logical operators

category: Basics
Created by: Dr.Ferrous

If conditionals, Comparative and Logical operators

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pinned 

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.



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 }



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...else statement executes some code if a condition is TRUE and another code if the condition is FALSE.



Becouse the $x variable isn't set, the "isset($x)" condition returns FALSE, this makes to be executed the code from "else" statement, and will output: "The $x variable is not defined".


An elseif clause allows you to add multiple conditions.

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.
Only the first condition that equates to TRUE will be executed; all others will be ignored, even if they're also True. This means you need to build conditional statements in the order of priority that you want them to be evaluated.

isset($x) returns TRUE only if $x 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).
Here, the "is_null($x)" returns TRUE, and the script will output: "x is NULL".

The PHP is_null($var) function finds whether the given variable is NULL. Returns TRUE if $var is null, FALSE otherwise.

Using if, else and elseif with Comparative and Logical Operators

Conditions often depend on the comparison of two values, and are used with comparative and logical operators.
The fallowing table shows the comparative and logical operators that can be used when writing conditionals.

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


The === operator (three '=') checks if two expresions are identical (if they have the same value and the same data type). For example (0==false) may return true, but (0===false) returns false.
- && returns True only if both $x and $y are true.
- || returns True if at least one of the expression $x and $y are true.
- !$x returns True if $x is false.

Examples with conditionals and operators.

Checks if two values are equals (the values can be strings too) [will dysplay "Hi Dr.Ferrous"]

Uses the "&&" and "||" operators to check two expresions [will dysplay "x<11 or y>32"]

You can nest conditionals. The next example will dysplay "It is a number less than 100 but greater than 10".

Ternary operator (the ? : operator)

The conditional operator ? ... : , called the ternary operator because it takes three operands (a condition, a result for true, and a result for false), is another way to make decisions in PHP.
This operator forms an expression which replaces the if ... else statement..


(condition) ? result_for_TRUE : result_for_FALSE;

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'.

This code can be expressed with an if ... else statement like this: