note 94813 added to control-structures.switch
| From: | dsysko at gmail dot com | Date: | Thu, 26 Nov 2009 08:17:30 +0000 |
| Subject: | note 94813 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163297@lists.php.net to get a copy of this message | ||
I frequently use conditional logic in my work, and I use fairly large automatically filled arrays.
This was getting tiresome, especially for longer matching sets:
function lotsacode ($i) {
switch ($i) {
case 'apple':
case 'pie':
case 5:
case 'tinned meat':
echo "$i is either a number or a food.\n";
break;
case 'frank':
case 'chimpanzee':
case 'spam':
echo "$i is a monkey, or smells like one.\n";
break;
}
}
Now, I do this:
function whatIsIt ($i) {
switch ($i) {
case (in_array($i , array('apple','pie',5))):
echo "$i is either a number or a food.\n";
break;
case (in_array($i , array('frank','chimpanzee'))):
echo "$i is a monkey, or smells like one.\n";
break;
}
}
lotsacode(5); // '5 is either a number or a food.'
whatIsIt('frank'); //'frank is a monkey, or smells like one.'
Here's what bothers me - neither the following, nor several variants I have tried, work:
$arrChoices = array('numberfood' => array('apple','pie',5) ,
'monkeys' => array('frank','chimpanzee'));
function notArraysWhat ($i) {
switch ($i) {
case (in_array($i , $arrChoices['numberfood'])):
echo "$i is either a number or a food.\n";
break;
case (in_array($i , $arrChoices['monkeys'])):
echo "$i is a monkey, or smells like one.\n";
break;
}
}
notArraysWhat('pie'); //Warning: in_array() expects parameter 2 to be array, null given...
----
Server IP: 64.71.164.2
Probable Submitter: 67.50.94.82
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/94813
Del: integrated -- https://master.php.net/note/delete/94813/integrated
Del: useless -- https://master.php.net/note/delete/94813/useless
Del: bad code -- https://master.php.net/note/delete/94813/bad+code
Del: spam -- https://master.php.net/note/delete/94813/spam
Del: non-english -- https://master.php.net/note/delete/94813/non-english
Del: in docs -- https://master.php.net/note/delete/94813/in+docs
Del: other reasons-- https://master.php.net/note/delete/94813
Reject -- https://master.php.net/note/reject/94813
Search -- https://master.php.net/manage/user-notes.php