If/Else Statements
       by kirupa : 27 June 2004

If/else statements are very useful because they allow your code to be expressed in certain situations but not in others. You can control when those situations occur by using control statements such as if/else.

Before we delve any further, a basic If / Else statement in PHP looks like the following:

<?php
if (true) {
do something
} else {
do something else
}
?>

Simply put, if a condition happens to be true, the code in pink will be executed. If the condition happens to be false, the code in orange, will be executed. The Else statement is optional though. If you do not specify an Else statement, your code will not do anything if the condition happens to be false.

For example, here is the code without an Else statement:

<?php
if (true) {
do something
}
?>

Let's take an example involving a robot called kirupaTron that is programmed to do among many other things, cross a busy intersection.

kirupaTron is at an intersection. He is programmed to wait for the Walk light before crossing the intersection. Once kirupaTron sees the Walk light come on, he knows it must be safe to cross the intersection. The logic behind his action is controlled by an If / Else statement.

The robot checks to see IF the Walk light turns on. If the Walk light is on, the robot decides to walk. IF the walk light is off, the robot decides to stay put. Lucky for us, kirupaTron is programmed in PHP, and the code that helps him at intersection looks like the following:

<?php
if ($light == "Walk") {
print("kirupaTron - start walking.");
} else {
print("kirupaTron - don't walk.");
}
?>

The above code is a simplified form that is similar to what is displayed above. If the variable, $light is set to Walk, the condition becomes true, and therefore, the code will execute. If the $light is set to something other than Walk, the condition fails, and the code for not walking will execute.

For example, copy and paste the following code into a new PHP document:

<?php
$light = "Stop";
if ($light == "Walk") {
print("kirupaTron - start walking.");
} else {
print("kirupaTron - don't walk.");
}
?>

When you preview the page containing the above code, you will see that the text for not walking is displayed. But why? The condition for the if/else statement to be true is when $light equals "Walk". Since in the previous line, we mention that $light equals "Stop", the condition fails. Therefore, the code for not walking executes.

Now, let's say you want to make the condition true. Well, in that case, replace the "Stop" with "Walk." Your first line of code should look like the following:

$light = "Walk";

Now, test your page again. You will find that the code for for  walking is displayed. The condition is true. The reason the condition is true is because the variable $light does equal "Walk."

In the next page we will cover more about if and else statements such as conditional and logical operators.

 


 

page 1 of 2


 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.