| PHP Programming The if Structure | The switch Structure |
Conditional structures are used to control which statements get executed. They are composed of three fundamental elements:
Conditionals in PHP are structured similarly to those found inC++ andJava. The structure begins with anif clause, which is composed of the word "if" followed by atrue/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 theif statement.elseif andelse clauses sometimes occur after theif clause to test for different statements.
Theif 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 anif statement is encountered, thetrue/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 theif clause are two optional clauses:else andelseif. Theelseif (orelse if) 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 precedingif statement comes out to be false. Otherwise they are skipped. Other than that, theelseif clause works just like a regularif clause. If it is true, its block is executed, if not, its block is skipped.
Finally, theelse clause serves as a "catch-all" for anif statement. Essentially theelse statement says "If all of the preceding tests fail, then execute this code."
<?php$foo=1;$bar=2;if($foo==$bar){echo"$foo is equal to$bar.";}elseif($foo>$bar){echo"$foo is greater than$bar.";}else{echo"$foo is less than$bar.";}?>
<?php$lower=10;$upper=100;$needle=25;if(($needle>=$lower)&&($needle<=$upper)){echo"The needle is in the haystack.";}elseif(($needle<=$lower)||($needle>=$upper)){echo"The needle is outside of the haystack.";}?>
Conditional values function via basic formal logic. It is important to understand how theif clause, among other clauses, evaluates these conditional values.
It is easiest to examine such withboolean values in mind, meaning that the result of a conditional value will be either TRUE or FALSE and not both. For example, if variable$x = 4, and a conditional structure is called with the expressionif ($x == 4), then the result of the expression will be TRUE, and theif structure will execute. However, if the expression is($x == 0), then the result will be FALSE, and the code will not execute. This is simple enough.
This becomes more complicated when complex expressions are considered. The two basic operators that expressions can be conjoined with are theAND (&&) andOR (||).
We are given variables$x and$y.
$x=4;$y=8;
Given the complex expression:
($x==4AND$y==8)
We are given a result of TRUE, because the result of both separate expressions are true. When expressions are joined with the AND operator, both sidesmust be true for the whole expression to be true.
Similarly:
($x==4OR$y==8)
We are given a result ofTRUE as well, because at least one expression is true. When expressions are joined with the OR operator, at least one sidemust be true for the whole expression to be true.
Conversely,
($x==4AND$y==10)
This expression will returnFALSE, because at least one expression in the whole is false.
However,
($x==4OR$y==10)
This expression will returnTRUE, because at least one expression in the whole is true.
A code block is one or more statements or commands that are contained between a pair of curly braces{ }. Blocks are used primarily in loops, conditionals and functions. Blocks can be nested inside one another, for instance as anif structure inside of a loop inside of a function.
If, after one of the conditional statements, there is no block of code enclosed by curly braces, only the next statement will be executed. It is recommended that you avoid using this to help prevent accidents when adding extra code after the block.
if(FALSE)echo'FALSE evaluates to true.';echo'Who knew that FALSE was TRUE?';
To avoid this problem, make sure to use brackets with conditional statements, even if there is only a single line of code to be executed. This prevents the error in the above code from occurring when you add an extra line after the existing block.
if(FALSE){echo'FALSE evaluates to true.';echo'Who knew that FALSE was TRUE?';}
If you are writing a long sentence where there are some parts non-static, you may create the string using if statement. The PHP syntax allows you to do this even within one line, using following shortcut syntax:
$money=535;# $print'I have'.($money>500?'':'n’t').' enough money.';
| PHP Programming The if Structure | The switch Structure |