Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Switch/Case ... Is this legal?

    Is this permitted in any version of PHP?

    PHP Code:
    switch ($var) {
    case 
    1: ...code...; break;
    case 
    2: ...code...; break;
    case 
    3,4,5,6: ...code...; break;
    default: 
    code;

    In particular I am refering to case 3,4,5: (numbers seperated by a comma).

    If not, if there another way around this?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 99 Times in 97 Posts
    not legal no but you can do this ...

    PHP Code:
    <? 
    switch (true) { 
        case 
    $var == : echo '1'; break; 
        case 
    $var == : echo '2'; break; 
        case 
    $var >&& $var : echo '3 - 6'; break; 
        default: echo 
    '>6 or 0'
    }
    ?>
    Last edited by firepages; 05-06-2004 at 03:54 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice one . Never seen that before and to be honest an if -elseif-elseif-else construct would do the same.

    But it's a nice trick.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •