note 88643 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Mon, 27 Jun 2022 20:58:14 +0000 |
| Subject: | note 88643 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218640@lists.php.net to get a copy of this message | ||
Note Submitter: sedativchunk at gmail dot com
----
Not sure if this has been posted or not, but I found the switch statement useful for finding ranges
of data.
This script creates web 2.0 style links in different font sizes (popular on blogs) using a
randomizer and switch statement. I used links from within a database for a mod I made for a Simple
Machines forums, but this example uses arrays for links if you wanted to add your own custom links:
<?php
// Create set of links
$link = array();
$link[] = '<a href="whatever.html">page 1</a>';
$link[] = '<a href="whatever.html">page 2</a>';
$link[] = '<a href="whatever.html">page 3</a>';
$link[] = '<a href="whatever.html">page 4</a>';
$link[] = '<a href="whatever.html">page 5</a>';
$link[] = '<a href="whatever.html">page 6</a>';
$link[] = '<a href="whatever.html">page 7</a>';
$link[] = '<a href="whatever.html">page 8</a>';
// Create loop to display links
for($i = 0; $i < count($link); ++$i)
{
// Create randomizer
// Use switch statement to find font size
$randomizer = rand(1,50);
switch($randomizer)
{
case ($randomizer <= 20):
$font_size = "11";
break;
case ($randomizer <= 30):
$font_size = "16";
break;
case ($randomizer <= 40):
$font_size = "18";
break;
case ($randomizer <= 50):
$font_size = "20";
break;
}
// Display the link
echo '<span style="font-size: ' .$font_size. ';">'
.$link[$i]. '</span> ';
// Loop the next link
}
?>
Using this type of range randomizer is useful for game development and it can be useful on the web
too, for things where you don't want to use a randomizer just for things like (1-5) where you
wanted a more then likely result for one thing over another. The switch statement saves from writing
spaghetti code if statements.