[PHP][Tutorial] Elseif

Results 1 to 15 of 15
  1. #1
    Vous Pas Reel HotelUnderSeige is offline
    MemberRank
    Nov 2007 Join Date
    ColoradoSpringsLocation
    1,290Posts

    [PHP][Tutorial] Elseif

    Lately i have seen people doing
    PHP Code:
    if ( var == var ) {
    echo 
    "cock";
    } else {
    if {var == var) {
    }

    That is not very secure, and can cuase errors. So today we learn the elseif statement :D.
    OK, it is very simple, first it needs an opening if statment.
    PHP Code:
    If ($Account == HotelUnder ) {
    echo 
    "hello HotelUnder";

    So perhaps for another acount you wanted it to say something else
    PHP Code:
    If ( $Account == HotelUnder ) {
    echo 
    "hello HotelUnder";
    } Elseif ( 
    $Account == Mental. ) {
    echo 
    "Welcome God";

    So that would be an alternate to doing
    PHP Code:
    if ( var == var ) {
    echo 
    "cock";
    } else {
    if {var == var) {
    }

    Hope it helped :D


  2. #2
    Valued Member ♠Comrade♠ is offline
    MemberRank
    Dec 2008 Join Date
    140Posts

    Re: [PHP][Tutorial] Elseif

    whoa thanks that will really help me in the Future


    Rated thread Exellent

  3. #3
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP][Tutorial] Elseif

    In reality it's the same thing.. It's just using the else statement with no brackets or space, then opening another if statement..

    Also, you don't even need to use brackets while dealing with 1 line of code, which is how 'elseif' works.

    I don't believe elseif is any different than the 2 functions 'else' and 'if' combined.

  4. #4
    Account Upgraded | Title Enabled! passie is offline
    MemberRank
    Jan 2005 Join Date
    The NetherlandsLocation
    710Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by HotelUnderSeige View Post
    PHP Code:
    If ($Account == HotelUnder ) {
    echo 
    "hello HotelUnder";

    you also can short the elseif up

    PHP Code:
    <?php
    $status 
    =  ($Account == admin ) ? "hello God" "hello guest";
    echo 
    $status;
    ?>

  5. #5
    Fuck you, I'm a dragon Pieman is offline
    MemberRank
    Apr 2005 Join Date
    The NetherlandsLocation
    7,414Posts

    Re: [PHP][Tutorial] Elseif

    Use indentation.

  6. #6
    The Gamma..? EliteGM is offline
    MemberRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP][Tutorial] Elseif

    Going on on Pieman;
    PHP Code:
    //Dont you dare capitalize if's

    if ($Account == "Some guy")
    {
        echo 
    "hello HotelUnder";
    }
    elseif (
    $Account == "OMG! Mental" )
    {
        echo 
    "Welcome God";


  7. #7
    Account Upgraded | Title Enabled! Mr.Lucifer is offline
    MemberRank
    Apr 2007 Join Date
    797Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by s-p-n View Post
    In reality it's the same thing.. It's just using the else statement with no brackets or space, then opening another if statement..

    Also, you don't even need to use brackets while dealing with 1 line of code, which is how 'elseif' works.

    I don't believe elseif is any different than the 2 functions 'else' and 'if' combined.
    If you use:

    Else {
    if ( x == x) )

    You're a crappy coder. It should always be as easy to possibly to read the code.

  8. #8
    Omega Ron is offline
    MemberRank
    Apr 2005 Join Date
    Location
    8,990Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Mr.Lucifer View Post
    If you use:

    Else {
    if ( x == x) )

    You're a crappy coder. It should always be as easy to possibly to read the code.
    They get the same result, whats the problem?

    On professional levels, you make it as easy as possible to read the code, thats understandable. Most of the people here use PHP as a personal hobby. As long as they can read their own code then it doesn't matter. Just because someone does something one way instead of another doesn't mean they're a bad coder. There are many ways to get the same result. People do what they prefer.

  9. #9
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Mr.Lucifer View Post
    If you use:

    Else {
    if ( x == x) )

    You're a crappy coder. It should always be as easy to possibly to read the code.
    It depends, I sometimes HAVE to use first an else, then if-else statements within the else. Makes code much more readable sometimes.

    PHP Code:
    if ($&& $<= $&& $> $5)
    {
    }
    else if ($
    && $<= $&& $<= !$5)
    {
    }
    else if ($
    && $> $&& !$> $5)
    {
    }
    else if ($
    && $> $&& !$<= !$5)
    {
    }
    else if ... 
    etc etc etc

    if ($1)
    {
        if ($
    > $3)
        {
            if ($
    > $5)
            {
            }
            else
            {
            }
        }
        else
        {
            if ($
    > $5)
            {
            }
            else
            {
            }
        }
    }
    else
    {

    Well, you get the point. Performance wise the second is often better.

  10. #10
    Account Upgraded | Title Enabled! Mr.Lucifer is offline
    MemberRank
    Apr 2007 Join Date
    797Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Ron View Post
    They get the same result, whats the problem?

    On professional levels, you make it as easy as possible to read the code, thats understandable. Most of the people here use PHP as a personal hobby. As long as they can read their own code then it doesn't matter. Just because someone does something one way instead of another doesn't mean they're a bad coder. There are many ways to get the same result. People do what they prefer.
    Sure, but these are the kind of habits you don't want to have if you ever want to work with someone on a bigger project. Or if you don't want to get laughed at when releasing source code.

    Quote Originally Posted by Daevius View Post
    It depends, I sometimes HAVE to use first an else, then if-else statements within the else. Makes code much more readable sometimes.

    Well, you get the point. Performance wise the second is often better.
    I found the first much more readable.

    I see no reason why their performance would differ.

  11. #11
    Account Upgraded | Title Enabled! King Izu is offline
    MemberRank
    Dec 2006 Join Date
    833Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Ron View Post
    They get the same result, whats the problem?

    Just because someone does something one way instead of another doesn't mean they're a bad coder. There are many ways to get the same result. People do what they prefer.
    Yes it certainly does. Bad code is code that is very suboptimal, or hard to read. Don't forget that the whole point of coding languages is to be able to read and understand it. They are many wrong ways of doing something, and this especially applies to PHP when people have this mentality that just because it works, it's okay to do it that way.

    Quote Originally Posted by Mr.Lucifer View Post
    I found the first much more readable.

    I see no reason why their performance would differ.
    The second does have better performance, although the first is a bit easier to read. The second's readability isn't significantly worse than the first. The performance differs because for example, if $1 was false, then the first version would continue to go through each and every else if statement, whereas the second version only checks $1 once, and since it is false, then it's body would not be executed.

  12. #12
    Apprentice Rekless is offline
    MemberRank
    Sep 2008 Join Date
    23Posts

    Re: [PHP][Tutorial] Elseif

    im actually pretty sure the elseif statement runs quicker sure theres very few times you'll ever notice it but hey a few billionths of a second is always nice to have =)

  13. #13
    Omega Ron is offline
    MemberRank
    Apr 2005 Join Date
    Location
    8,990Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Mr.Lucifer View Post
    Sure, but these are the kind of habits you don't want to have if you ever want to work with someone on a bigger project. Or if you don't want to get laughed at when releasing source code.
    Quote Originally Posted by King Izu View Post
    Yes it certainly does. Bad code is code that is very suboptimal, or hard to read. Don't forget that the whole point of coding languages is to be able to read and understand it. They are many wrong ways of doing something, and this especially applies to PHP when people have this mentality that just because it works, it's okay to do it that way.
    As I said, for personal projects which is just about what everyone that posts code on here is ever going to do, using "if {} else { if {}" and "else if" aren't going to make any difference.

    Besides, there is nothing bad about using it with the statements separated. It is often easier to add in different if statements inside of an if block with the first example. Having code on its own lines and everything tabbed properly is way easier to read than a bunch of "else if" lines one after the other.

    Quote Originally Posted by King Izu View Post
    Don't forget that the whole point of coding languages is to be able to read and understand it.
    You just contradicted yourself. Using if and else separately is a lot easier to read than back to back "else if" lines, therefore by that point the first option is better. Refer to Daevius's post for a great example of this.

  14. #14
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP][Tutorial] Elseif

    Quote Originally Posted by Rekless View Post
    im actually pretty sure the elseif statement runs quicker sure theres very few times you'll ever notice it but hey a few billionths of a second is always nice to have =)
    1.) elseif() is exactly the same as saying else if(). And as Daevius and that King Izu person explained, once the if statement is declared true, the else statement is ignored. Once the if statement is declared false, everything inside the brackets of if() are ignored, and it just starts reading from the else bracket.

    So the 2 boxes of code are exactly the same in function, the first one is 2 brackets faster. Whitespace doesn't count against you.
    PHP Code:
    if(Adam Bangs Eve) {
       
    gotoAndStopprincipals office.
    } else if(
    Eve Hugs Adam) {
       
    gotoAndStopdetention.
    } else {
       
    Not Messing Around.

    PHP Code:
    if(Adam Bangs Eve) {
       
    gotoAndStopprincipals office.
    } else {
       if (
    Eve Hugs Adam) {
          
    gotoAndStopdetention.
       } else {
          
    Not Messing Around.
       }

    2.) There is no elseif() function. It's just the two functions else and if combined without brackets. Thus, else { if(){} } is 2 brackets slower than elseif(){}. 2 brackets slower is not a calculatable speed by any human. A computer would take longer to count that speed than it would take to load 2 brackets. So in essence, it's best to make it look comprehendable. Speed is not an issue here. Just concentrate on organization skills, and speed will normally come along for the ride.
    Last edited by s-p-n; 16-01-09 at 03:21 AM. Reason: reason 2 for posting

  15. #15
    Vous Pas Reel HotelUnderSeige is offline
    MemberRank
    Nov 2007 Join Date
    ColoradoSpringsLocation
    1,290Posts

    Re: [PHP][Tutorial] Elseif

    Kinda off topic but my tutorial was signature worthy :D



Advertisement