Conditional structures are used to control which statements get executed. They are composed of three fundamental elements:
if statements;
elseif statements; and
else statements.
Conditionals in PHP are structured similarly to those found in C++ and Java. The structure begins with an if clause, which is composed of the word "if" followed by a true/false statement in parentheses ( ). The subsequent code will be contained in a block, denoted by curly braces { }. Sometimes the braces are omitted, and only one line will follow the if statement. elseif and else clauses sometimes occur after the if clause, to test for different statements.
The if clause says "If this statement is true, I want the program to execute the following statements. If it is false, then ignore these statements." In technical terms, it works like this: When an if statement is encountered, the true/false statement in parentheses is evaluated. If the statement is found to be true, the subsequent block of code contained in curly braces is executed. However, if the statement is found to be false, the program skips those lines and executes the next non-blank line.
Following the if clause are two optional clauses: else and elseif. The elseif clause says "If the last statement was false, let's see if this statement is true. If it is, execute the following code. If it isn't, then skip it." elseif statements are only evaluated when the preceding if statement comes out to be false. Otherwise they are skipped. Other than that, the elseif clause works just like a regular if clause. If it is true, its block is executed, if not, its block is skipped.
Finally, the else clause serves as a "catch-all" for an if statement. Essentially the else statement says "If all of the preceding tests fail, then execute this code."
Example 1
<?php $num1= 1; $num2= 2; if($num1 == $num2) { echo "$num1 is equal to $bar."; } elseif ($num1 > $num2) { echo "$num1 is greater than $bar."; } else { echo "$num2 is less than $bar."; } ?<
How They Work
Here's an example of a simple game where a user enters a $user_command and different functions are run as a resul
if($user_command == "n") { go_north(); } else if($user_command == "e") { go_east(); } else if($user_command == "s") { go_south(); } else if($user_command == "w") { go_west(); } else { do_something_else(); }
Clearly, there's a lot of repeated code here. The switch case structure allows you to avoid this redundant code. It allows programmers to repeatedly compare the value of a certain variable to a list of possible values and execute code based on the result. This is the syntax for a switch case statement, compared to the same code written using if statements:
if statement style switch case style
if($firstvariable == 'comparison1' || $firstvariable == 'comparison2') { doSomething(); doSomethingElse(); } else if ($firstvariable == 'comparison3') { doAThirdThing(); } else { launchMissiles(); }
// Look at how much switch case saves you!
switch($firstvariable) { case 'comparison1': case 'comparison2': doSomething(); doSomethingElse(); break; case 'comparison3': doAThirdThing(); break; default: launchMissiles(); break; } The switch case style will save you from retyping $firstvariable, and make your code look cleaner (especially if that code is a long chain of simple if statements). Returning to our zork sample program, we have:Original Code Switch-Case Code if($user_command == "n") { go_north(); } else if($user_command == "e") { go_east(); } else if($user_command == "s") { go_south(); } else if($user_command == "w") { go_west(); } else { do_something_else(); } switch($user_command) { case 'n': go_north(); break; case 'e': go_east(); break; case 's': go_south(); break; case 'w': go_west(); break; default: do_something_else(); break; }We are known for Website Development and Website Designing, along with Android iOS application development in Mumbai, India.