Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Solved: php boolean operators in switch


(!)

andynic's Avatar
andynic andynic is offline
Computer Specs
Member with 232 posts.
THREAD STARTER
 
Join Date: May 2007
Location: Amsterdam
Experience: Beginner
06-May-2012, 03:50 AM #1
Solved: php boolean operators in switch
I don't see comparison and boolean operators in case clauses of the switch statement documented in the php documentation (i.e.www.php.net/manual).

The code below seems to work as one would expect. But is it valid and supported php code?
I'm using on Mac OS 10.6:
PHP 5.3.8 (cli) (built: Dec 5 2011 21:24:09)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

I want to be sure this syntax will be supported in future versions of php.

Thanks for your help.
Andynic
<?php

$a = "3abc";

switch ($a)
{
case ($a == "1" || $a == "2"):
echo "a is 1 or 2<br />";
break;

case (stripos($a, '3') !== false ):
echo "a is 3 or 4<br />";
break;

case "5":
echo "a is 5<br />";
break;

case "6":
echo "a is 6<br />";
break;

default:
echo "dflt<br />";
}
?>
allnodcoms's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 610 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
06-May-2012, 05:59 AM #2
Thumbs up Yup, It's fine...
According to the manual:
Quote:
The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.
All of your examples fall within these rules (as true and false are just ints) so I see no problems...

Danny
andynic's Avatar
andynic andynic is offline
Computer Specs
Member with 232 posts.
THREAD STARTER
 
Join Date: May 2007
Location: Amsterdam
Experience: Beginner
06-May-2012, 11:23 AM #3
Thanks very much, Danny. I missed that sentence you pointed out.
Andynic
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
boolean, case, comparison, php, switch

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑