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 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cases vs. if/else statements

    i asked a question about this kinda a while ago, but now i'm wondering if using cases, ie:

    PHP Code:
    switch($information) {
    case 
    1:
    $destination "Good";
    break;
    case 
    2:
    $destination "Bad";
    break;
    default:
    $destination "Ok";
    break;

    instead of

    PHP Code:
    if ($information == 1) {$destination "Good"}
    elseif (
    $information == 2) {$destination "Bad"}
    etc... 
    I'm currently using the if/elseif way, but i have around 200 or those so i think it's making the site load very slow, so i'm wondering if using the cases way would make it faster? Thanks

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when i check for a serie of values of 1 variable (like in your case), i always use switch (with break for each option).
    i use If-elsif etc for real conditional checks on values of more then 1 variable.

    the manual on switch() says something similar http://be.php.net/manual/en/control-...res.switch.php

    I suppose it is faster but never tested it.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    lol , I do the opposite , less than 3 options I use if/else , any more I use switch , but thats just a coding preference for me rather than anything else , I dont know which is faster if any ?
    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)

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i do the same as you firepages, but I always use switch loops if im setting what the script would run... like in a CMS system, I switch $_GET['action'] to find out what to do.

    As for speed, im not sure... never tested it.


  •  

    Posting Permissions

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