Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Spoono Source Crave Tutorial Database Photoshop Star 

Change Language

Who's online

There are currently 0 users and 7 guests online.

Lesson 8 - Control Structures 2

Watch The Movie of Lesson 8

else and elseif

Now that you understand IF lets look at two other control structures that are used in an IF statement - else and elseif. (The good thing about PHP is that you can kind of guess what each function or control structure does just by looking at its name). Lets start with else.

Quote:
Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is NOT met. This is what else is for. Else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. - php.net

Taking our previous lesson's code we could change it to:

<?php
If ($your_number $cpu_number) {
   
// "if" $your_number is greater than $cpu_number do the following:
   
echo "Your Number Is Bigger!";
} else {
   
// "if" $your_number is less than $cpu_number do the following:
   
echo "Your Number Is Smaller!";
}
?>

Here is a logic example:

<?php
if (expression true) {
   
then do statement
} else {//If the "if" above is FALSE
   
then do this statement
}
?>

Read the following simple if and else statements outloud and you will be able to hear the logic and understand it better.

<?php
If (2) {
    echo 
"1 is bigger than 2!";
} else {
    echo 
"1 is NOT bigger than 2!"//This would print
}

//Human Version:

If (one is greater than two) {
   
Then print this to the screen "1 is bigger than 2!";
} else { 
//I guess one is smaller than two so...
   
Then print this to the screen "1 is NOT bigger than 2!"//This would print
}


// Using the FALSE value

If (FALSE) {
    echo 
"The if is FALSE!";
} else {
    echo 
"So the else is TRUE!"//This would print
}

// Using the TRUE value
If (TRUE) {
    echo 
"The if is TRUE!"//This would print
} else {
    echo 
"So the else is FALSE!";
}
?>

elseif

Quote:
elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. - php.net

Taking lesson seven's previous code we could change it to:

<?php
/* Make a new variable called "$cpu_number" 
and set it equal to a random number between 0 and 100. */
$cpu_number rand(0100);

// Make a new variable called "$your_number" and set it equal to "50".
$your_number 50;

    If (
$your_number == $cpu_number) { 
   
       
//"if" $your_number is equal to $cpu_number do the following:
       
echo "Your number the same!";
       
    } elseif (
$your_number $cpu_number) { 
   
       
/* "if" $your_number is greater than $cpu_number do the following: */
       
echo "Your Number Is Bigger! CPU number is $cpu_number";
       
    } else {
   
       
//The only option left is ($your_number < $cpu_number) So....
        // "if" $your_number is less than $cpu_number do the following:
       
echo "Your Number Is Smaller! CPU number is $cpu_number";
       
    }
?>

Logic example:

<?php
if (expression true) {
   
then do statement
} elseif (this expression true) {
   
then do this statement
} else {//"if" everything else is FALSE
   
then do this statement
}
?>

Quote:
There may be several elseif's within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE. - php.net

So as you can see, the else and elseif are very useful in keeping extra IF's out of your code. Take the following as an example:

<?php
If (2) {
    echo 
"1 is equal to 2!";

If (
>= 1) {
    echo 
"2 is greater than or equal to 1!";
}
If (
== 2) {
    echo 
"2 is equal to 2!";
}
/////////////////
// Using ELSE
/////////////////
If (2) {
    echo 
"1 is equal to 2!";
} elseif (
>= 1) {
    echo 
"2 is greater than or equal to 1!";
   
//the IF ends here because this is true.
} elseif (== 2) {
   
//this is also true but the loop ended when it found the true value above!
   
echo "2 is equal to 2!";
   
}
?>

When you start running multiple IF's in your code you leave it open to bugs because you take a chance on having a weird statement that could be true in more than one of the [/i]IF[/i]'s. (like the code above). So by placing your code in a looooooong IF made up of else's and elseif's you can make sure that only one of the statements will be true and after that statement is run - the rest of the statement (the other else's and elseif's) will be skipped. (this is assuming you only want one statement to run.)

Here are some more examples of control structures to help you better understand them:

<?php
$fruit 
'apple';
if (
$fruit == 'pear') {
    echo 
"$fruit is a pear!";
}
elseif (
$fruit == 'banana') {
    echo 
"$fruit is a banana!";
}
else {
    echo 
"then $fruit must be an apple because it is not a banana or pear!";
}
?>

This would output the last 'else' statement because the first two Control Structures were FALSE. ($fruit = 'apple', not 'banana' or 'pear'.)

For more information please read these articles:

PHP: Conditionals
PHP Looping
Looping The Loop
the While Loop
Loops

If you find a broken link please PM me, thanks!


Submitted by David on November 9, 2006 - 6:09pm.
printer friendly version

Movie in lesson 8

Hi,

I am simply amazed at how good your movie approach is.
It’s really helped me to pick up PHP at a rate I did not think was possible.

I am however having problems viewing lesson 8, it does not run like the others.

Please help.

Once again, this is a really great approach to teaching php


Fixed

Sorry about that, it appears there was no reference to the movie in our file. It has been added though. :)


Lesson 8

I am a senior still trying to learn PHP and have been searching the web, trying and not getting past the "post". Finally I have found a site that I can understand and grasp the meaning off. I am extremely greatful for your tutorials and lessons. I now "know" that I will be able to do!!!
Noticed one mistake(at least I think so) here in this lesson;

Taking lesson seven's previous code we could change it to: (there is one too many "}" on the end)
Again, thank-you:
russ


Thank you

I am very glad to hear that you are enjoying the tutorials! I have been a little busy lately with CodeXplorer but I plan to start writing (or recording) more lessons soon.

I also fixed the extra "}"...


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati