Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1
    Join Date
    Dec 2006
    Posts
    3,970
    Tokens
    0

    Latest Awards:

    Default Which is quicker?

    This;
    PHP Code:
    if ( $id == "1" ) {
    }
    else {
         if ( 
    $id == "2" ) {
         }
         else {
              if ( 
    $id == "3" ) {
              }
              else {
              }
         }

    or this;
    PHP Code:
    if ( $id == "1" ) {
    }
    else if ( 
    $id == "2" ) {
    }
    else if ( 
    $id == "3" ) {
    }
    else {

    Im using the 2nd one alot arround my site and i was wondering if the first way is quicker.
    Lets set the stage on fire, and hollywood will be jealous.

  2. #2
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    2nd one

  3. #3
    Join Date
    Dec 2007
    Posts
    1,683
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Tom743 View Post
    This;
    PHP Code:
    if ( $id == "1" ) {
    }
    else {
         if ( 
    $id == "2" ) {
         }
         else {
              if ( 
    $id == "3" ) {
              }
              else {
              }
         }

    or this;
    PHP Code:
    if ( $id == "1" ) {
    }
    else if ( 
    $id == "2" ) {
    }
    else if ( 
    $id == "3" ) {
    }
    else {

    Im using the 2nd one alot arround my site and i was wondering if the first way is quicker.
    Second version, as I think if else's are slower than elseifs.

  4. #4
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    Surely the best way would be to use switch and cases....

    PHP Code:
    $id 2;

    switch (
    $id) {
    case 
    1:
        echo 
    "ID has the value of 1";
        
    //add query/function/echo etc...
        
    break;
    case 
    2:
        echo 
    "ID has the value of 2";
        
    //add query/function/echo etc...
        
    break;
    case 
    3:
        echo 
    "ID has the value of 3";
        
    //add query/function/echo etc...
        
    break;

    That would bring up case 2 as the number of $id is 2....

    Hope that helps, seems a more logical way of doing it to me. However from the 2 that you posted, 2 wouldn't be any faster but the better way todo it.
    Last edited by Source; 12-07-2008 at 01:47 AM.


    www.fragme.co = a project.

  5. #5
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default

    Second one, I'm sure =)
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


  6. #6
    Join Date
    Dec 2007
    Posts
    1,683
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by TomSpit View Post
    Second one, I'm sure =)
    No, they both take the same time. Matt's solution is a lot cleaner code and faster.

    Switches are a lot better than 100 else if/if else's.

  7. #7
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    Note my user tag "$learning::php( "slowly" );" - thus meaning I am learning php slowly. So the switch option would be the best option for me personally, but I may be wrong in terms of it been better, though im 90% sure.


    www.fragme.co = a project.

  8. #8
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Note my user tag "$learning::php( "slowly" );" - thus meaning I am learning php slowly. So the switch option would be the best option for me personally, but I may be wrong in terms of it been better, though im 90% sure.
    Meh. Sadly most of the php coders on this forum won't understand that that's valid php (your usertag).

  9. #9
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    haha, they will all learn soon enough surely?

    I love the name : "Paamayim Nekudotayim" (the :: for none php coders)


    www.fragme.co = a project.

  10. #10
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    haha, they will all learn soon enough surely?
    That's what I'm hoping for!

Page 1 of 2 12 LastLast

Posting Permissions

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