PHP Tutorial - If Else
PHP Tutorial - If Else
The PHP if statement is very similar to other programming languages use of the if statement , but for those who are not familar with it, picture the following:
Think about the decisions you make before you go to sleep. If you have something to do the next day, say go to work, school, or an appointment, then you will set your alarm clock to wake you up. On the other hand, if you do not have something tomorrow, then you will not worry about the alarm clock.
This simple kind of if/then statement is very common in every day life and also appears in programming quite often. Whenever you want to make a decision given that something is true (you have something to do tomorrow) and be sure that you take the appropriate action, you are using an if/then relationship.
The if statement is quite useful in programming, thus it is important in PHP. Imagine that you want to print out "Happy New Year!", on January 1st, at the top of your homepage only on January 1st. With the use of PHP if statements you could have this process automated, months in advance, occuring every year on January 1st.
This idea of planning for future events is something you would never have had the opportunity of doing with HTML.
If Statement Example
The Happy New Years example would be a little difficult for you to do right now, so let us instead start off with the basics of the if statement. The PHP if statement tests to see if a value is true, and if it is a segment of code will be executed. See the example below for the form of a PHP if statement.
PHP Code:
$my_name = "someguy";
if ( $my_name == "someguy" ) {
echo "Your name is someguy!<br />";
}
echo "Welcome to my homepage!";
Display:
Welcome to my homepage!
Did you get that we were comparing the variable $my_name with "someguy" to see if they were equal? Additionally, otice that because the if statement turned out to be true, the code segment was executed, printing out "Your name is someguy!". Let's go a bit more in-depth into this example to iron out the details.
- We first set the variable $my_name equal to "someguy".
- We next used a PHP if statement to check if the value contained in the variable $my_name was equal to "someguy"
- The comparison between $my_name and "someguy" was done with a double equal sign "==", not a single equals"="! A single equals is for assigning a value to a variable, while a double equals is for checking if things are equal.
- Translated into english the PHP statement ( $my_name == "someguy" ) is ( $my_name is equal to "someguy" ).
- $my_name is indeed equal to "someguy" so the echo statement is executed.
A False If Statement
Let us now see what happens when a PHP if statement is not true, in other words, false. Say that we changed the above example to:
PHP Code:
if ( $my_name == "someguy" ) {
echo "Your name is someguy!<br />";
}
echo "Welcome to my homepage!";
Display:
Here the variable contained the value "anotherguy", which is not equal to "someguy". The if statement evaluated to false , so the code segment of the if statement was not executed. When used properly, the if statement is a powerful tool to have in your programming arsenal!
If/Else Conditional Statment
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.
NHow 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 make a conditional statement to check, "are you visiting for the first time". If the condition is true, then ask them 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.
If/Else an Example
Using these conditional statements can add a new layers of "cool" to your website. Here's the basic form of an if/else statement in PHP.
PHP Code:
if ( $number_three == 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}
Display:
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.
False Makes Use of Else
Oon the other hand, if the if statement was false, then the code contained in the else segment would have been executed, instead. 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.
PHP Code:
if ( $number_three == 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}
Display:
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.