note 104490 added to control-structures.switch
| From: | php_net at mcdragonsoftware dot com | Date: | Fri, 17 Jun 2011 16:53:58 +0000 |
| Subject: | note 104490 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180648@lists.php.net to get a copy of this message | ||
I often find myself wanting an inline-switch statement (like the ?: for inline if) or
"choose" function, to pick a single value out of a list based on a variable index... just
a convenience function really, but I like compact code...
function iswitch( $index, $OPTIONS ) {
return @$OPTIONS[$index-1];
}
usage:
echo iswitch( $roll, array( 'rock', 'paper', 'scissors' ) );
I know there is a function for returning a random element from an array, and on a pre-defined array
you can pull the item at a given index. But there is no method for quickly grabbing item x out of a
list inline. (unless there are only 2 options, then the ?: operator works fine)
----
Server IP: 69.147.83.197
Probable Submitter: 67.0.3.235
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/104490
Del: integrated -- https://master.php.net/note/delete/104490/integrated
Del: useless -- https://master.php.net/note/delete/104490/useless
Del: bad code -- https://master.php.net/note/delete/104490/bad+code
Del: spam -- https://master.php.net/note/delete/104490/spam
Del: non-english -- https://master.php.net/note/delete/104490/non-english
Del: in docs -- https://master.php.net/note/delete/104490/in+docs
Del: other reasons-- https://master.php.net/note/delete/104490
Reject -- https://master.php.net/note/reject/104490
Search -- https://master.php.net/manage/user-notes.php