note 39155 deleted from control-structures.switch by crell

From: Date: Mon, 27 Jun 2022 20:37:11 +0000
Subject: note 39155 deleted from control-structures.switch by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218631@lists.php.net to get a copy of this message
Note Submitter: gmgiles at pacbell dot net ---- 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. <?php $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.]

« previous php.notes (#218631) next »