Posted by : senan Wednesday, February 12, 2014

Welcome! In this section we are going to learn how to write conditional statements in PHP using "if elseif and else" clause.

Often in programming while writing code to solve problems, we require to perform certain actions based on certain conditions. For example, in our application, we might want to display different messages to the users on our page based on what day of the week it is. In order to handle this, we write what we call conditional statements where each action is performed based on a condition and when that condition is true. Here's what i mean...
if (condition)
 perform action 1
else
 perform action 2
The above pseudo-code translates... if the first condition is true, then perform action 1, otherwise, perform action 2.

The If...Else Statement in PHP

Let's examine some code to get an idea how conditional statements are done in PHP. In the following example, we evaluate what day of the week it is today. Based on that we display an appropriate message to the user.

Example - If..Else Statement in PHP (wihtout curly brackets)

<html>
<body>

<?php
 //Give what day of the week it is. Returns Sunday through Saturday.
 $day = date("l");
 
 if ($day == "Saturday")
  echo "It's party time :)";
 else
  echo "Ahhh! I hate work days.";
?>

</body>
</html>
In the above example, we first determine what day of the week it is, then write a small if statement (conditional statement) to see if today is Saturday. If it is true, then the code will output "It's party time :)", else (if it is not saturday), the second condition will come true and the code will output "Ahhh! I hate work days.".
Pretty neat 'eh :). Let's move on...
PHP If Esle Using Curly Brackets
In the above example, we wrote one line of code inbetween the if and else statements. What if we want to write more then one line of code in there? Well, for that we will have to remember to use the "{" curly brackets. You must enclose the action part of the code in curly brackets. Failing to do that will cause an error. Let's tweak our first example to see how this is done.

Example - IF Else Using Curly Brackets

<html>
<body>

<?php
 //Give what day of the week it is. Returns Sunday through Saturday.
 $day = date("l");
 
 if ($day == "Saturday")
 {
  echo "It's party time :)";
  echo " Where are you going this evening?";
 }
 else
 {
  echo "Ahhh! I hate work days.";
  echo " I want weekend to come :)";
 }
?>

</body>
</html>
In the above example, we use the curly brackets to enclose the action part of the conditional statements when we have more than one line in there.

The Elseif Statement in PHP

In the above section, you saw how to write two conditional statements using "If and Else". What if we want to have more than two conditional statements? We can have as many conditional statements as we want in your code. To do that, we have to use "elseif" between our "if" and "else" statements. Let's say in our above example we also want to print a message for Fridays to our users saying "Have a nice weekend!". We would use the elseif clause to do that. Let's see how that is done.

Example - The Elseif Statement

<html>
<body>

<?php
 //Give what day of the week it is. Returns Sunday through Saturday.
 $day = date("l");
 
 if ($day == "Saturday")
 {
  echo "It's party time :)";
  echo " Where are you going this evening?";
 }
 elseif ($day == "Friday")
 {
  echo "Have a nice day!";
 }
 else
 {
  echo "Ahhh! I hate work days.";
  echo " I want weekend to come :)";
 }
?>

</body>
</html>
Now the users will see "Have a nice day!" if the today is Friday. Pretty handy eh :)
So, as you saw, if..else conditional statements can help us write some logic and help us take decisions in our code. You will find yourself writing conditional statements a lot in your code. So, make sure you grasp this topic. You will see the use of conditional statements a lot in the rest of the tutorials.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -