PHP conditional logic
You can implement PHP code which makes decisions based on certain things using conditional logic.
This tutorial focuses on:
- The if statement
- The else statement
- The else-if statement
- Using if, else, and else if together
- The switch statement
- The ternary operator
The if statement
The if statement tests if a certain condition is true or false and acts upon it accordingly.
If the condition in the parenthesis is true, then the code following the condition will be executed, otherwise it will not.
NOTE: Use two equal signs (==) in the condition when comparing values. One equal sign (=) is used to assign values, while two equal signs (==) are used to compare values.
The else statement
If the condition in the parenthesis in an if statement is true, then the code following the condition will be executed. But what if you wanted something to happen if it is false? What if you wanted one thing to happen if the condition is true, and something else to happen if the condition is false? This is where the else statement comes in. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.
If the condition in the if statement is false, then the action dictated by the else statement will be performed.
The else-if statement
The if statement tests a single condition and performs an action if that condition is true and the else statement performs an action if the condition in the if statement is false, but what if there are more than two possibilities? Surely, any condition can be only true or false, but what if you needed to test a variable for more than one value? This is where the else if statement comes in. The else if statement is used in conjunction with the if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement is false, but rather it performs an action if the condition in the if statement is another specific value specified in the else if statement itself.
Using if, else, and else if together
You can use the if, else, and else if statements together when you want to check a variable for a certain value many times. If it is not any of the checked values then the code specified by the else statement will be executed.
NOTE: When using if, else if, and else statements - if the action dictated by these statements contains more than one line of code, it should be sorrounded by curly braces, otherwise the curly braces are optional. Although, curly braces should always be used with conditional statements. Doing so is good convention.
In the above example, some of the conditional statements dictate actions with one line of code - not sorrounded by curly braces, and some conditional statements dictate actions with more than one line of code - sorrounded by curly braces.
The switch statement
The switch statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, else if, else structure. There is an important keyword used within the switch statement, and that keyword is the break keyword. The break keyword is used to make sure that the switch statement will not fall through to the next possible value, even if that value is incorrect within the switch statement.
The ternary operator
The ternary operator is the question mark symbol (?), it works the same way as the if-else structure.