Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home If...else If...Else - PHP


If...Else - PHP

                                PHP If...Else Statements

 

Conditional statements are used to perform different actions based on different conditions.


Has someone ever told you, "if you work hard, then you will succeed"? And what happens if you do not work hard? Well, you fail! This is an example of an if/else conditional statement.
If you work hard then you will succeed.
Else, if you do not work hard, then you will fail.
 

How does this translate into something useful for PHP developers?

 

 

Well consider this:

 

Someone comes to your website and you want to ask this visitor her name if it is her first time coming to your site. With an if statement this is easy. Simply have a conditional statement to check, "are you visiting for the first time". If the condition is true, then take them to the "Insert Your Name" page, else let her view the website as normal because you have already asked her for her name in the past.

 

Another example:

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. 

 

In PHP we have the following conditional statements:

  • if statement - use this statement to execute some code only if a specified condition is true
  • if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
  • if...elseif....else statement - use this statement to select one of several blocks of code to be executed
  • switch statement - use this statement to select one of many blocks of code to be executed

 

  • The if Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax

                                                        if (condition) code to be executed if condition is true;

 

The following example will output "Have a nice weekend!" if the current day is Friday:

 Code:

                                                                       <?php

                                                                                 $d=date("D");
                                                                                  if ($d=="Fri") echo "Have a nice weekend!";
                                                                       ?>

 

Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true.

 

  • The if...else Statement

Let's see another example :

Code:

                                                                        <?php

                                                                                  $number_three = 3;

                                                                                   if ( $number_three == 3 ) {
                                                                                         echo "The if statement evaluated to true";
                                                                                   } else {
                                                                                         echo "The if statement evaluated to false";
                                                                                   }

                                                                         ?>

 

The if statement evaluated to true

 

This is a lot to digest in one sitting, so let us step through the code, line by line.

  • We first made a PHP variable called $number_three and set it equal to 3.
  • In this example we compared a variable to an integer value. To do such a comparison we use "==", which in English means "Is Equal To".
  • $number_three is indeed Equal To 3 and so this statement will evaluate to true.
  • All code that is contained between the opening curly brace "{" that follows the if statement and the closing curly brace "}" will be executed when the if statement is true.
  • The code contained within the else segment will not used.
  • On the other hand, if the if statement was false, then the code contained in the else segment would have been executed. Note that the code within the if and else cannot both be executed, as the if statement cannot evaluate to both true and false at one time! Here is what would happen if we changed to $number_three to anything besides the number 3.

Code: 

                                                                             <?php

                                                                                       $number_three = 421;

                                                                                        if ( $number_three == 3 ) {
                                                                                                echo "The if statement evaluated to true";
                                                                                        } else {
                                                                                                echo "The if statement evaluated to false";
                                                                                        }

                                                                             ?>

 

The if statement evaluated to false

 

The variable was set to 421, which is not equal to 3 and the if statement was false. As you can see, the code segment contained within the else was used in this case.

Remember:  If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

Code: 

                                                                              <?php

                                                                                        $d=date("D");
                                                                                         if ($d=="Fri")
                                                                                            {
                                                                                              echo "Hello!<br />";
                                                                                              echo "Have a nice weekend!";                    // this is called a block of code
                                                                                              echo "See you on Monday!";
                                                                                            }

                                                                              ?>

 

  • The if...elseif....else Statement

Use the if....elseif...else statement to select one of several blocks of code to be executed.

 

Syntax

                                                    if (condition)
                                                        code to be executed if condition is true;
                                                    elseif (condition)
                                                        code to be executed if condition is true;
                                                   
else
                                                        code to be executed if condition is false;

 

An if/else statement is great if you only need to check for one condition. However, what would you do if you wanted to check if your $employee variable was the company owner Bob, the Vice President Ms. Tanner, or a regular employee? To check for these different conditions you would need the elseif statement.

 

  • PHP - elseif What is it?

An if statement is made up of the keyword "if" and a conditional statement (i.e. $name == "Ted"). Just like an if statement, an elseif statement also contains a conditional statement, but it must be preceded by an if statement. You cannot have an elseif statement without first having an if statement.

When PHP evaluates your If...elseif...else statement it will first see if the If statement is true. If that tests comes out false it will then check the first elseif statement. If that is false it will either check the next elseif statement, or if there are no more elseif statements, it will evaluate the else segment, if one exists (I don't think I've ever used the word "if" so much in my entire life!). Let's take a look at a real world example.

 

  • PHP - Using elseif with if... else

Let's start out with the base case. Imagine we have a simpler version of the problem described above. We simply want to find out if the employee is the Vice President Ms. Tanner. We only need an if else statement for this part of the example.

Code: 

                                                                           <?php

                                                                                    $employee = "Bob";
                                                                                     if($employee == "Ms. Tanner"){
                                                                                            echo "Hello Ma'am";
                                                                                     } else {
                                                                                            echo "Morning";
                                                                                     }

                                                                            ?>

 

Now, if we wanted to also check to see if the big boss Bob was the employee we need to insert an elseif clause.

Code:

                                                                            <?php

                                                                                      $employee = "Bob";
                                                                                      if($employee == "Ms. Tanner"){
                                                                                              echo "Hello Ma'am";
                                                                                      } elseif($employee == "Bob"){
                                                                                              echo "Good Morning Sir!";
                                                                                      }else {
                                                                                              echo "Morning";
                                                                                      }

                                                                             ?>

 

An output will be:

                                                     Good Morning Sir!

 

PHP first checked to see if $employee was equal to "Ms. Tanner", which evaluated to false. Next, PHP checked the first elseif statement. $employee did in fact equal "Bob" so the phrase "Good Morning Sir!" was printed out. If we wanted to check for more employee names we could insert more elseif statements!

Remember that an elseif statement cannot be used unless it is preceded by an if statement!

 

 

    PHP SWITCH STATEMENT