boolean and comparison operators in switch

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
andynic
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Feb 29, 2012 4:33 am

Sun May 06, 2012 12:00 am

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, using various values for $a, 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 />";
}
?>
User avatar
freshnet
php-forum GURU
php-forum GURU
Posts: 365
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Mon May 07, 2012 10:11 am

I would say that it's valid code, and I can't see a reason why it would be unsupported later. Maybe you'd be better off preconverting your $a in situations like this so that it was just a number, if only to make the case statement less confusing.
User avatar
minimihi
php-forum GURU
php-forum GURU
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Mon May 07, 2012 2:13 pm

It works, but you loose speed and have exceptions using switch like that.

Since you write conditions inside of case, you should use boolean values in switch expression.
For example try setting

Code: Select all

$a = "";
switch will look for case which returns false. In your case that would be first case :?

Also, you would have to change 'case "5"' and 'case "6"' to case conditions.
Since switch expression value true/false is compared to each case.

e.g.:

Code: Select all

switch(true) {
    case ($a == "1" || $a == "2"):
        blablabla...
} 

If anyone is interested. I made speed test for conditional switches and else-if in type safe and strict type comparison. I'm attaching code file.
Here is one of results I'm getting with 10 000 iterations

Code: Select all

Type safe:
 Bool-Switch: 0.06105899810791
 Switch: 0.062847137451172
 Else-if: 0.036962032318115
Strict type:
 Bool-Switch: 0.049129962921143 (safe/strict hybrid)
 Switch: 0.050456047058105 (safe/strict hybrid)
 Else-if: 0.026036977767944
Numbers are in second.
Attachments
cond-switch and else-if test.zip
cond-switch and else-if speed test
(567 Bytes) Downloaded 56 times
AtifShahabQureshi
New php-forum User
New php-forum User
Posts: 21
Joined: Tue Oct 27, 2015 6:05 am

Tue Oct 27, 2015 6:24 am

First of all Its a valid PHP code. It will show the following output:

a is 3 or 4

I would suggest you to use ternary operators. It will definitely reduce the lines of code.
Post Reply