PHP Conditional Statements
Conditional statements are used to execute different code blocks based on different conditions. A conditional evaluates a Boolean expression, that is, something that returns a value. If the expression is true, it will execute everything inside its block of code. A block of code is a group of statements enclosed by {}.
Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Conditions are used in complex statements to determine whether a block of simple statements should be executed. To set up conditions, you compare values using comparison operators.
Examples of Conditions
- Are two values equal?
- Is one value larger or smaller than another?
- Does a string match a pattern?
- Does the ZIP code have five numeric characters?
You can also set up conditions in which you ask two or more questions.
Comparing Values
You can compare numbers or strings to see whether they are equal, whether one is larger than the other, or whether they are not equal. You compare values with comparison operators. PHP evaluates the comparison and returns true or false.
You can compare both numbers and strings. Strings are compared alphabetically, with all uppercase characters coming before any lowercase characters. For example, SS comes before Sa.
Strings are compared based on their ASCII code. In the ASCII character set, each character is assigned an ASCII code that corresponds to a number between 0 and 127. When strings are compared, they are compared based on this code. For example, the number that represents the comma is 44. The period corresponds to 46. Therefore, if a period and a comma are compared, the period is evaluated as larger.
Checking Variable Content
Sometimes you just need to know whether a variable exists or what type of data is in the variable.
isset($varname): True if variable is set, even if nothing is stored in it.
empty($varname): True if value is 0 or is a string with no characters in it or is not set.
You can also test what type of data is in the variable.
- is_int($number)
- is_array($var2)
- is_float($number)
- is_null($var1)
- is_numeric($string)
- is_string($string)
You can test for a negative condition, as well, by using an exclamation point (!) in front of the expression.
Joining Multiple Comparisons
Often you need to ask more than one question to determine your condition. This requires you to join comparisons, which have the following the general format:
comparison1 and|or|xor comparison2 and|or|xor comparison3
Comparisons are connected by one of the following three words:
- and: Both comparisons are true.
- or: One or both comparisons are true.
- xor: Only one of the comparisons is true.
Conditional Statements
A conditional statement executes a block of statements only when certain conditions are true. Two useful types of conditional statements are:
-
An if statement: Sets up a condition and tests it. If the condition is true, a block of statements is executed.
-
A switch statement: Sets up a list of alternative conditions. It tests for the true condition and executes the appropriate block of statements.
1. Using if Statements
A conditional is defined by the keyword if followed by a Boolean expression in parentheses and by a block of code. If the expression is true, it will execute the block, otherwise it will skip it. You can increase the power of conditionals by adding the keyword else. This tells PHP to execute some block of code if the previous conditions were not satisfied.
You can also add an elseif keyword followed by another condition and a block of code to continue asking PHP for more conditions. You can add as many elseif as you need after an if. If you add an else, it has to be the last one of the chain of conditions. As soon as PHP finds a condition that resolves to true, it will stop evaluating the rest of conditions.
if (condition)
{
block of statements
}
elseif (condition)
{
block of statements
}
else
{
block of statements
}
Alternative Syntax
PHP has an alternative syntax for the conditional statements. In this syntax, the if statement’s opening bracket is replaced with a colon, the closing bracket is removed, and the last closing bracket is replaced with the endif keyword.
if ($x == 1): echo "x is 1";
elseif ($x == 2): echo "x is 2";
else: echo "x is something else";
endif;
2. Using switch Statements
Another control structure similar to if…else is switch…case. This structure evaluates only one expression, and executes the block depending on its value.
For most situations, the if conditional statement works best. However, sometimes you have a list of conditions and want to execute different statements for each condition.
switch ($variable_name)
{
case value:
block of statements;
break;
case value:
block of statements;
break;
...
default:
block of statements;
break;
}
The switch statement tests the value of $variable_name. The script then skips to the case section for that value and executes statements until it reaches a break statement or the end of the switch statement. If there is no case section for the value of $variable_name, the script executes the default section.
When the case matches the current value of the expression, PHP executes the code inside it. As soon as PHP finds a break statement, it exits the switch…case. In case none of the cases are suitable for the expression, PHP executes the default, if there is one, but that is optional.
The breaks are mandatory if you want to exit the switch…case. If you do not specify any, PHP will keep on executing statements, even if it encounters a new case.
Alternative Syntax
The switch statement also has an alternative syntax, which uses the endswitch keyword to terminate the statement.
switch ($x):
case 1: echo "x is 1"; break;
case 2: echo "x is 2"; break;
default: echo "x is something else";
endswitch;
The alternative syntax is often preferable for longer conditional statements since it then becomes easier to see where those statements end.
Ternary Operator
In addition to the if and switch statements, there is the ternary operator (?:) . This operator can replace a single if/else clause. The operator takes three expressions. If the first one is evaluated to true, then the second expression is returned, and if it is false, the third one is returned.
Ternary operator expression:
$y = ($x == 1) ? 1 : 2;
In PHP, this operator be used as an expression and as a statement.
Ternary operator statement:
($x == 1) ? $y = 1 : $y = 2;
The programming term expression refers to code that evaluates to a value, whereas a statement is a code segment that ends with a semicolon or a closing curly bracket.