Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 13 of 13

Thread: <?PHP ?>: If Statements in Switch cases

  1. #1

    <?PHP ?>: If Statements in Switch cases

    Is this actually possible? I thought I saw this before. Not sure if it was real or not. If so, then how does it work?


  2. #2
    case "something":
    if ($true)
    {
    echo('it was true');
    }
    break;

    ... I'm quite confused though what your talking about.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    Quote Originally Posted by Templarian View Post
    case "something":
    if ($true)
    {
    echo('it was true');
    }
    break;

    ... I'm quite confused though what your talking about.
    Like a switch() having a IF statement in it.


  4. #4
    Like:
    PHP Code:
    switch($a == $b $c $d){

    ?

  5. #5
    Quote Originally Posted by blazes View Post
    Like:
    PHP Code:
    switch($a == $b $c $d){

    ?
    No he means inside each "case" not the condition for the switch itself.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  6. #6
    1,839
    posts
    Registered User
    PHP Code:
    switch($myVar){
        case 
    'a'
              if(
    $sessionVar 'joran rules'){
                        echo 
    $sessionvar;
              }
              break;
        case 
    'b':
              if(
    $sessionVar 'joran rules'){
                        echo 
    $sessionvar.",  NOT!!";
              }
              break;
          



  7. #7
    Why are you using switches in the first place?

  8. #8
    1,839
    posts
    Registered User
    switches are better than a buncha ifs(not sure about performance wise, maybe someone else knows, but definatly easier to dealwith...at least imho)...but he probably wants to check some global var from inside..

  9. #9
    According to all my speed tests, switchs are far faster than a bunch of ifs, and the more ifs you'd have, the faster it gets.

  10. #10
    I've read that switches are slower so I never use them. *shrug*

  11. #11
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    Switches might be faster, but they're generally employed be people not-so-in-the-know for things that could be done using completely different methods, faster.

    What are you trying to do with the switch statement, bear?
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  12. #12
    Quote Originally Posted by icio View Post
    Switches might be faster, but they're generally employed be people not-so-in-the-know for things that could be done using completely different methods, faster.

    What are you trying to do with the switch statement, bear?
    I disagree, the switch construct is a very useful one when employed correctly. It's great to use instead of 5 or more if statements personally.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  13. #13
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    I disagree, the switch construct is a very useful one when employed correctly.
    Ie. By people in-the-know. People not-in-the-know tend to use them over more practiced and generally prefered techniques which don't require switches to be used at all. I made no point over their usage with regards to if-statements.

    I thought I was pretty clear on this. Sorry for your confusion.
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012