note 39155 added to control-structures.switch
| From: | gmgiles at pacbell dot net | Date: | Mon, 19 Jan 2004 06:07:47 +0000 |
| Subject: | note 39155 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63812@lists.php.net to get a copy of this message | ||
Did you know that switch() and case() can also accomodate things like basic math calculations and
counter incrementing? They do. In this example, I use a switch statement (which is inside of a while
loop) to alternate the background color of a table row. It gives me a cool spool-printer-paper
effect.
$rows_per_color = 5; // change bgcolor every 5 rows
switch($ctr++) {
case 0:
$bgcolor = "#ffffff";
break;
case ($rows_per_color):
$bgcolor = "#ff0000";
break;
case ($rows_per_color * 2):
$bgcolor = "#ffffff";
$ctr = 1;
break;
}
As you can see, I increment $ctr by 1 in the switch() itself, and the final case() does a simple
calculation. Simple, but powerful. [Remember, the above example is inside of a while() loop... each
time it iterates, switch increments $ctr.]
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39155
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39155&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39155&report=yes
Search -- http://master.php.net/manage/user-notes.php