Re: [PHP][Tutorial] Elseif
whoa thanks that will really help me in the Future
Rated thread Exellent
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.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
HotelUnderSeige
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;
?>
Re: [PHP][Tutorial] Elseif
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";
}
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
s-p-n
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.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Mr.Lucifer
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.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Mr.Lucifer
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 ($1 && $2 <= $3 && $4 > $5)
{
}
else if ($1 && $2 <= $3 && $4 <= !$5)
{
}
else if ($1 && $2 > $3 && !$4 > $5)
{
}
else if ($1 && $2 > $3 && !$4 <= !$5)
{
}
else if ... etc etc etc
if ($1)
{
if ($2 > $3)
{
if ($4 > $5)
{
}
else
{
}
}
else
{
if ($4 > $5)
{
}
else
{
}
}
}
else
{
}
Well, you get the point. Performance wise the second is often better.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Ron
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
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.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Ron
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
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.
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 =)
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Mr.Lucifer
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
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
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.
Re: [PHP][Tutorial] Elseif
Quote:
Originally Posted by
Rekless
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) {
gotoAndStop, principals office.
} else if(Eve Hugs Adam) {
gotoAndStop, detention.
} else {
Not Messing Around.
}
PHP Code:
if(Adam Bangs Eve) {
gotoAndStop, principals office.
} else {
if (Eve Hugs Adam) {
gotoAndStop, detention.
} 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.
Re: [PHP][Tutorial] Elseif
Kinda off topic but my tutorial was signature worthy :D