๐ PHP Conditionals
Conditional Statements
Conditional statements are used for decision making and flow control. The condition determines which statements execute.
The if statement is the most commonly used decision-making structure.
<?php
if (conditional expression) {
statement(s);
}// end if
?>
(Note:some programmers like to put // end if after the closing brace. // indicates a comment. This code is not necessary for the program to function, but it does make your code more readable, so you may want to adopt this style)
Conditional expressions use variables and literals linked together by a comparison operator.
Example: $A >= $B
Operator Type | Operators | |
Comparison |
== |
equal |
Important note: You must use a double equal sign in an if statement to compare two equal values. If you don't, PHP will actually assign the value of B to A which can give unpredictable results and make debugging extremely difficult!
Statement(s) can be any PHP code that should be executed if the condition is true.
<?php
if ($grade >= 90) {
$letterGrade = "A";
}
?>
Statements inside theย curly braces { } are only executed if the condition evaluates to true. Any statements after the closing curly brace } are executed whether or not the statement is true.
Logical operators are used to form compound if conditions.
Operator Type | Operators | |
Logical |
&& (AND)
|| (OR) ! (NOT) |
Returns true if both the left and right operand return a value of true. |
You can use any of the comparison operators and logical operators in your conditional expressions.
<?php
if ($grade >= 80) && ($grade < 90) {
$letterGrade = "B"; // executed only if grade is 80 - 89
}
print "You made a $letterGrade"; // always executed regardless of grade value
?>
If there is only one line of code to be executed, you don't have to include the braces, but it's good form to always include it to make your code more readable and consistent.
if . . . else Statements
You can include an else clause in an if statement to run an alternate set of code if the conditional expression is false.
<?php
if ($grade >= 90) {
print "You made an A"; // executed if grade is 90 - 100
} else {
print "Sorry, you did not make an A"; // executed if grade is < 90
}
?>
Nested if Statements
You can include an if statement inside of another if or if...else statement.
<?php
if ($grade >= 90) {
print "You made an A"; // grade is 90 - 100
} elseif ($grade > 80) {
print "You made a B"; // grade is 80 - 89
} elseif ($grade > 70) {
print "You made a C"; // grade is 70 - 79
} elseif ($grade > 60) {
print "You made a D"; // grade is 60 - 69
} else {
print "You failed"; // grade is < 60
}
?>
Switch Statement
Switch statements control program flow by executing a specific set of statements depending on the value of an expression.The switch statement is a great alternative to the if statement, when you have a single variable that you want to test against several values. For example, if you were calculating sales tax, you could use the state code to determine the tax rate using the switch statement instead of a lot of if's.
<?php
switch (expression) {
case label:
statement(s);
break;
case label:
statement(s);
break;
default:
statement(s);
}
?>
- An expression can be any valid PHP expression, usually a variable or arithmetic expression.
- The label can be a literal value or variable name.
- Statement(s) can be any PHP code that should be executed if the expression is equal to the label.
- Break causes execution of the case statement to end.
- Default statements execute if no other case statements are matched.
Here is the same example we wrote in the last chapter with nested if statements. As you can see, the case statement is cleaner and easier to follow:
<?php
switch ($grade) {
case "A":
print "You are very smart";
break;
case "B":
print "You are a good worker";
break;
case "C":
print "You did OK";
break;
case "D":
print "You passed (barely)";
break;
default:
print "You failed";
}
?>