The if/else condition is the most simple form of conditional processing and you will find it in any programming language. So if you already know some other programming language this will be an easy read. If not, just read this tutorial carefully because it will be useful when learning any other programming language.
Did you paid attention to what I've just said? The upper paragraph contains a conditional statement. Let's translate that to pseudo-code. By the way, pseudo-code is a simplified way of writing natural language code without taking into consideration the programming-specific syntax.
php
Cool isn't it? PHP have the following conditional statements when it comes to performing different actions depending if a condition is true or false
It worth to mention that while, for and their derived are also called loops, because they execute a piece of code while the condition is met.
Great, now let's be more PHP specific and translate the above example to valid if/else statement syntax.
php$know_js = false;
if($know_js == true) {
echo "I know Javascript. This read will be easy";
} else {
echo "I am a complete beginner. I will read this carefully";
}
By looking at the above example we can say that this is the if statement syntax
phpif (condition) {
// execute this following code if condition is true
}
Here are some examples of simple conditions that can be evaluated to true or false inside the if statement
phpif ( $x > 1 )
if ( $x != 5 )
if ( $a == "string" )
if ( $a !== false )
if ( $x > 2 and $x < 8 )
You can read more about the comparison and logical operators in the PHP comparison operators tutorial.
Like I said, the if / else statement executes some code if a condition is true and another code if that condition is false.
phpif (condition) {
// execute this following code if condition is true
} else {
// execute this following code if condition is false
}
The if / elseif / else leaves the door open for the developer to take into consideration more then two conditions.
phpif (condition 1) {
// execute this following code if condition is true
} elseif (condition 2) {
// execute this following code if condition 2 is true
} else {
// execute this following code if condition 1 and condition 2 are both evaluated to false
}
The IF / ELSE statements are easy to use but they can they can sometimes become be too long for a simple thing to code. Here is where the ternary operators comes in by using "(condition) ? true : false" expressions.
Here is a simple example to look at:
php$x = 5;
$a = ($x > 4 ? true : false); // returns true
The advantages of ternary logic is obvious: You improve readability while your code becomes simpler, shorter and easier to maintain.
php$level = 2;
echo 'May the force be with you '.($level > 4 ? "my young padawan" : "master Jedi" ).'. How are you today?';
// this will print: "May the force be with you, master Jedi. How are you today?"
Let's take the previous example and complicate it a little bit
php$level = 2;
$level = 10;
echo 'May the force be with you '.($level > 4 ? "my young padawan" : ( $level != 10 ? "master Jedi" : "master Yoda" ) ).'. How are you today?';
// this will print: "May the force be with you master Yoda. How are you today?"
This can sometimes get quite complicated if you include more and more conditions. So just stop and think that the shorthand if / else has the purpose to simplify and make things easier. In this case, you will have to find the balance between the short and the standard form.
This is it for now. I will leave the switch statement and loops for a separate tutorial.