Loops and PHP Decision Making
(Page 1 of 5 )
In this third part of a three-part series on PHP, you will learn about loops and more. This article is excerpted from chapter four of the book
Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
The ? Operator
The ? operator is a ternary operator, which means it takes three operands. It works like an if statement but returns a value from one of the two expressions. The conditional expression determines the value of the expression. A colon (:) is used to separate the expressions, as shown here:
{expression} ? return_when_expression_true : return_when_expression_false;
Example 4-8 tests a value and returns a different string based on whether it's TRUE or FALSE.
Example 4-8. Using the ? operator to create a message
<?php
$logged_in = TRUE;
$user = "Admin";
$banner = ($logged_in==TRUE)?"Welcome back, $user!":"Please login.";
echo "$banner";
?>
Example 4-8 produces:
Welcome back, Admin!
This can be pretty useful for checking errors. Now, let's look at a statement that lets you check an expression against a list of possible values to pick the executable code.
The switch Statement
The switch statement compares an expression to numerous values. It's very common to have an expression, such as a variable, for which you'll want to execute different code for each value stored in the variable. For example, you might have a variable called $action, which may have the values add, modify, and delete. The switch statement makes it easy to define a block of code to execute in response to each of those values.
To illustrate the difference between using the if statement and the switch statement to test a variable for several values, we'll show you the code for the if statement (in Example 4-9), and then for the switch statement (in Example 4-10).
Example 4-9. Using if to test for multiple values
if ($action == "ADD") {
echo "Perform actions for adding.";
echo "As many statements as you like can be in each block.";
}
elseif ($action == "MODIFY") {
echo "Perform actions for modifying.";
}
elseif ($action == "DELETE") {
echo "Perform actions for deleting.";
}
Example 4-10. Using switch to test for multiple values
switch ($action) {
case "ADD":
| echo "Perform actions for adding.";
echo "As many statements as you like can be in each block.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
}
The switch statement works by taking the value after the switch keyword and comparing it to the cases in the order in which they appear. If no case matches, the code isn't executed. Once a case matches, the code is executed. The code in subsequent cases also executes until the end of the switch statement or until a break keyword. This is useful for processes that have several sequential steps. If the user has already done some of the steps, he can jump into the process where he left off.
The expression after the switch statement must evaluate to a simple type, such as a number, an integer, or a string. An array can be used only if a specific member of the array is referenced as a simple type.
There are numerous ways to tell PHP not to execute cases besides the matching case.
Next: Breaking out >>
More Programming Basics Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.
|
|