note 39155 modified in control-structures.switch by vrana

From: Date: Tue, 17 Aug 2004 13:54:25 +0000
Subject: note 39155 modified in control-structures.switch by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80041@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. <?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.] --was-- 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.] http://php.net/manual/en/control-structures.switch.php

« previous php.notes (#80041) next »