Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 69
Comments: 20 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
if, elseif, else
Programmers often need to perform various tasks only if a certain condition is met. Learn how to use the if, elseif, and else control structures to do this.

if, elseif, and else control structures

When using a programming language there is most often a need to do different tasks based on different situations. The method used to do this sometimes varies, but in PHP it is quite easy to do.

We use the if, elseif, and else control structures for this. Here is an example of how if/elseif/else statements come together:

<?php

// I could just do an if, and stop there

if(this_condition_is_true) { 
    
// perform this task
}

// or I can continue and add an elseif

// if the above was FALSE, test this condition
elseif(this_condition_is_true) {
    
// perform this task
}

// I can go even further and add an else

// if the above tests fail
else {
    
// perform this task
}

?>

I'll go through this step-by-step to explain what is going on. First off, the code starts with an if statement. This tells PHP to evaluate the expressions inside the parenthesis and if it evaluates to TRUE, then PHP executes the lines of code within the curly braces that immediately follow. I say expressions because you can have virtually any number of conditions for each if statment (more on this later).

Next, if the first if statement returns FALSE then PHP moves onto the elseif statement. If does the same thing here that it did with the initial if statement. If the expressions inside of the parenthesis evaluate to TRUE it performs the lines inside of the curly braces that immediately follow.

Finally, if both the if and the elseif statements have failed, PHP defaults to the else statement. Notice that the else does not have conditions to follow. PHP simply executes the code in the curly braces.

This is the basic structure for any if/elseif/else statement. Using them to your advantage mostly involves the operators you use with your conditional tests.

Conditional and Logical operators

More often then not, conditional statments will use one or more operators to complete the conditional test. The kind of operators we'll use for our conditional tests are comparison operators and logical operators. For more information on operators visit the PHP manual.

Let's try out some basic if statements.

<?php

// Setup some variables for our examples

$a 5;
$b 6;
$c 7;

$animal "cow";
$sound "moo";

$animal2 "bird";
$sound2 "tweet";

// Example of an if only

if($c == || $c == 8) {
    echo 
"The number is 7 or 8.";
}

// Example of an if/else

if($a == $b) {
    echo 
"The two numbers are equal!";
} else {
    echo 
"The two numbers are not equal!";
}

// Example of an if/elseif/else

if($animal == "cow" && $sound == "moo") {
    echo 
"The animal is a cow!";
}
elseif(
$animal2 == "bird" && $sound2 == "tweet") {
    echo 
"The animal is a bird!";
} else {
    echo 
"The animal is not a cow or a bird.";
}

?>

These are rather vague examples, but their purpose is to illustrate the different structures of conditional statements as well as to introduce some of the operators that can be used within the conditional expressions.

If you take a look at the second if/elseif/else statement, lines 23-27, you will notice that it does not contain an elseif expression. When using conditional statements you do not have to use each part. However, you must have the if portion.

The operator used in the second if/elseif/else statement (==) is the comparison operator "Equal". It simply compares the two values and returns TRUE or FALSE depending on those values. There are many other comparison operators that you may use, including "Less than" (<), "Greater than" (>), "Less than or equal to" (≤), "Greater than or equal to" (≥) and so forth. For a complete list of comparison operators, have a look at the PHP manual.

The final if/elseif/else statement, lines 31-38, is basically the same as the previous. The only difference here is the addition of the elseif portion and the logical operator for "AND" (&&).

The "AND" operator allows you to include more conditions that the entire expression must have in order for it to return TRUE or FALSE. There is also a logical operator for "OR" (||). It is used in the same way that "AND" is used, but has the opposite effect. For a complete list of logical operators, have a look at the PHP manual.

That's pretty much it for conditionals. They're pretty easy to use, you just have to try them out yourself and play with the different operators. If you have any questions, feel free to discuss this tutorial below.

Discuss Tutorial: if, elseif, else 1 Comment
Comment by Ferimer5 on Jul 23, 2006, 7:10 am
I'm going to test it out on my site Wink

I say going as going because when time passes, I will...

« Previous [ 1 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.