note 88643 added to control-structures.switch
| From: | sedativchunk at gmail dot com | Date: | Mon, 02 Feb 2009 05:22:22 +0000 |
| Subject: | note 88643 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150087@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 67.163.248.210
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/88643
Del: integrated -- https://master.php.net/note/delete/88643/integrated
Del: useless -- https://master.php.net/note/delete/88643/useless
Del: bad code -- https://master.php.net/note/delete/88643/bad+code
Del: spam -- https://master.php.net/note/delete/88643/spam
Del: non-english -- https://master.php.net/note/delete/88643/non-english
Del: in docs -- https://master.php.net/note/delete/88643/in+docs
Del: other reasons-- https://master.php.net/note/delete/88643
Reject -- https://master.php.net/note/reject/88643
Search -- https://master.php.net/manage/user-notes.php