PHP Programming/The if Structure
Conditional Structures
editThe if statement
editConditional structures are used to control which statements get executed. They are composed of three fundamental elements:
- if statements;
- elseif statements; and
- if 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 (or else 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 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
edit<?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.";
}
?>
Example 2
edit<?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 expressions
editConditional values function via basic formal logic. It is important to understand how the if clause, among other clauses, evaluates these conditional values.
It is easiest to examine such with boolean 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 expression if ($x == 4), then the result of the expression will be TRUE, and the if 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 the AND (&&) and OR (||).
Examples
editWe are given variables $x and $y.
$x = 4;
$y = 8;
Given the complex expression:
($x == 4 AND $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 sides must be true for the whole expression to be true.
Similarly:
($x == 4 OR $y == 8)
We are given a result of TRUE as well, because at least one expression is true. When expressions are joined with the OR operator, at least one side must be true for the whole expression to be true.
Conversely,
($x == 4 AND $y == 10)
This expression will return FALSE, because at least one expression in the whole is false.
However,
($x == 4 OR $y == 10)
This expression will return TRUE, because at least one expression in the whole is true.
Code Blocks
editA 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 an if 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?';
}
Shorthand notation
editIf 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.';
For more information
edit