Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
PHP 101 (part 3): Looping The Loop
Learn how to repeatedly execute code with loops and more sophisticated conditional tests.

| Switching Things Around |

An alternative to the if-else() family of control structures is PHP's switch-case() statement, which does almost the same thing. It looks like this:


switch (decision-variable) {
    case first condition is true:
        do this!
    case second condition is true:
        do this!
      ... and so on...
}


Depending on the value of the decision variable, the appropriate case() block is executed. A default block can also be created, to handle all those occasions when the value of the decision variable does not match any of the listed case() conditions.

I'll make this a little clearer by re-writing one of my earlier examples in terms of the switch() statement:

[code]
<html>
<head></head>
<body>

<?php

// get form selection
$day = $_GET['day'];
// check value and select appropriate item
switch ($day) {
    case 1:
        $special = 'Chicken in oyster sauce';
        break;
    case 2:
        $special = 'French onion soup';
        break;
    case 3:
        $special = 'Pork chops with mashed potatoes and green salad';
        break;
    default:
        $special = 'Fish and chips';
        break;
}

?>

<h2>Today's special is:</h2>
<?php echo $special ?>
</body>
</html>
[code]

There are a couple of important keywords here:

* The break keyword is used to break out of the switch() statement block and move immediately to the lines following it.

* The default keyword is used to execute a default set of statements when the variable passed to switch() does not satisfy any of the conditions listed within the block.

A common newbie mistake here is to forget the break at the end of every case() block. Remember that if you forget to break out of a case() block, PHP will continue executing the code in all the subsequent case() blocks it encounters.

For more on the switch() statement, see http://www.php.net/manual/en/control-structures.switch.php.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback