Who's Online
We have 7 guests onlineBreadcrumbs
| If...Else - PHP |
PHP If...Else Statements
Conditional statements are used to perform different actions based on different conditions.
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:
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");
Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true.
Let's see another example : Code: <?php $number_three = 3; ?>
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.
Code: <?php $number_three = 421; ?>
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!"; } ?>
Use the if....elseif...else statement to select one of several blocks of code to be executed.
Syntax if (condition)
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.
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.
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"; ?>
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"; ?>
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!
|