Re: select function
| From: | Stuart Dallas | Date: | Thu, 25 Oct 2012 21:47:38 +0000 |
| Subject: | Re: select function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319596@lists.php.net to get a copy of this message | ||
On 25 Oct 2012, at 22:35, "Jeff Burcher" <jeff@allredmetal.com> wrote:
> I can't remember if this is the PHP list for RPG programmers or not, so
> apologize if this is the wrong one.
Not an RPG list, but your question is PHP-related more than it's RPG-related.
> Is there an equivalent command in PHP for the SELECT statement in RPG? I see
> switch, which is a little similar, but far from how it actually functions.
>
> For those not familiar with the SELECT statement here is how I envision it
> working in a PHP format similar to switch:
>
> SELECT {
> WHEN $Auth = 0:
> WHEN $A = 1:
> echo('$Aprint_list');
> WHEN $B = 1:
> echo('$Bprint_list');
> WHEN $A = 2:
> echo('$Aprint_list');
> echo('$Aprint_list');
> WHEN $B = 2:
> echo('$Bprint_list');
> echo('$Bprint_list');
> DEFAULT:
> }
>
> The syntax may be a little off, but you get the picture. No breaks are
> needed because it only performs the first match it comes to, then exits the
> SELECT statement when finished the commands under that match. Putting a WHEN
> statement with nothing to perform means exactly that, if this matches, do
> nothing, then exit the SELECT statement. I hope that makes sense. The big
> difference is you can put anything behind each WHEN statement as opposed to
> looking at only the one variable like with switch. I am thinking I just need
> to get creative with the switch or elseif commands, but thought I would ask
> the list first if they had any better suggestions.
You don't need to "get creative" with anything as that's exactly how switch
works if you have a break at the end of each case. So your example would look like so:
switch (true) {
case $Auth == 0:
break;
case $A == 1:
echo('$Aprint_list');
break;
case $B == 1:
echo('$Bprint_list');
break;
case $A == 2:
echo('$Aprint_list');
echo('$Aprint_list');
break;
case $B == 2:
echo('$Bprint_list');
echo('$Bprint_list');
break;
default:
break;
}
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/