# Solved: php boolean operators in switch



## andynic (May 25, 2007)

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
";
break;

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

case "5":
echo "a is 5
";
break;

case "6":
echo "a is 6
";
break;

default:
echo "dflt
";
}
?>


----------



## allnodcoms (Jun 30, 2007)

According to the manual:


> 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 (May 25, 2007)

Thanks very much, Danny. I missed that sentence you pointed out.
Andynic


----------

