Upload
lotlot
View
1.244
Download
0
Embed Size (px)
Citation preview
PHPConditional Logic
The if Statement
An if statement is a way of controlling the execution of a statement that follows it (that is, a single statement or a block of code inside braces). The if statement evaluates an expression between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely. This enables scripts to make decisions based on any number of factors.
if (expression) { // code to execute if the expression evaluates to true }
Sample #1
<?php $satisfied = "very"; if ( $satisfied == "very" ) { echo "We are pleased that you are happy
with our service"; // register customer satisfaction in some way } ?>
Using the else Clause with the if Statement When working with the if statement, you will often want
to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code, like so:
if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases }
Sample #2
<?php // $satisfied = "very";if ( $satisfied == "very" ) {echo "We are pleased that you are happy with our
service"; // register customer satisfaction in some way } else { echo "Please take a moment to rate our service"; // present pulldown } ?>
Using the else if Clause with the if Statement You can use an if/else else/if construct to test multiple
expressions before offering a default block of code:
if ( expression ) { // code to execute if the expression evaluates to true } else if ( another expression ) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases }
Sample #3<?php
$satisfied = "no";
if ( $satisfied == "very" ) {
echo "We are pleased that you are happy with our service";
// register customer satisfaction in some way
} else if ( $satisfied == "no") {
echo "We are sorry that we have not met your expectations";
// request further feedback
} else {
echo "Please take a moment to rate our service";
// present pulldown
}
?>
The switch Statement The switch statement is an alternative way of changing program flow according
to the evaluation of an expression. . Using the if statement in conjunction with else if, you can evaluate multiple expressions. Switch evaluates only one expression, executing different code according to the result of that expression, as long as the expression evaluates to a simple type (a number, string, or Boolean).
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}
Sample #4<?php
$satisfied = "no";
switch ( $satisfied ) {
case "very":
echo "We are pleased that you are happy with our service";
break;
case "no“:
echo "We are sorry that we have not met your expectations";
break;
default:
echo "Please take a moment to rate our service"; 21:
}
?>
Thank You!