PHP Tutorial – 07 – Conditionals
The conditional statements are used to execute different code based on different conditions.
If statement
The if statement will execute only if the expression inside parenthesis is true. In PHP, this does not have to be a boolean expression.
if ($x == 1) echo "x is 1";
To execute more than one statement conditionally they must be grouped together with curly brackets.
if ($x == 1) { echo "x is "; echo $x; }
The if statement can be extended with any number of elseif clauses to test for other conditions. Each additional condition will only be tested if all previous conditions are false. Finally, the if statement can have an else clause at the end, which will execute if all previous conditions are false.
if ($x == 1) echo "x is 1"; elseif ($x == 2) echo "x is 2" else echo "x is something else";
Switch statement
The switch statement compares an expression with a series of values. It can be used to write the same conditional statements as above, but the expression for the switch statement can only be used for integers, floats, or strings. Also, it will only check for equality whereas with the if statement any conditional operators can be used.
switch ($x) { case 1: echo "x is "; echo $x; break; case 2: echo "x is 2"; break; default: echo "x is something else"; }
Note that the statements after each case value are not surrounded by curly brackets. Instead, the statements end with the break keyword to skip the rest of the cases.
Alternative syntax
PHP has an alternative syntax for the conditional statements. In this syntax the opening brace is replaced with a colon, the closing braces are removed, and the last closing brace is replaced with the endif keyword. This makes it look similar to the switch statement.
if ($x == 1): echo "x is $x"; elseif ($x == 2): echo "x is 2"; else: echo "x is something else"; endif;
The switch statement also has an alternative syntax similarly to the if statement, but here the endswitch keyword is used to terminate the statement.
switch ($x) : case 1: echo "x is "; echo $x; break; case 2: echo "x is 2"; break; default: echo "x is something else"; endswitch;
The alternative syntax is often preferable for longer statements since it becomes easier to see where that statement ends.
Ternary operator
In addition to the if and switch statements there’s the ternary operator (?:) that can replace a single if/else clause. This operator takes three expressions. If the first one is true the second expression is evaluated and returned, and if it’s false the third one is.
// Ternary operator expression $y = ($x == 1) ? 1 : 2;
In PHP, this operator can not only be used as an expression, but also as a stand-alone statement.
// Ternary operator statement ($x == 1) ? $y = 1 : $y = 2;
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)


It’s spooky how clerve some ppl are. Thanks!