1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: php boolean operators in switch

Discussion in 'Software Development' started by andynic, May 6, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    300
    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 />";
    }
    ?>
     
  2. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    According to the manual:
    All of your examples fall within these rules (as true and false are just ints) so I see no problems...

    Danny
     
  3. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    300
    Thanks very much, Danny. I missed that sentence you pointed out.
    Andynic
     
As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1052157