note 94813 deleted from control-structures.switch by danbrown
| From: | danbrown@php.net | Date: | Thu, 26 Nov 2009 18:17:23 +0000 |
| Subject: | note 94813 deleted from control-structures.switch by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163312@lists.php.net to get a copy of this message | ||
Note Submitter: dsysko at gmail dot com
----
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...