What's the difference and which are you supposed to use?
I typically stick to else if
unless I am using short tags. Is this correct??
What's the difference and which are you supposed to use?
I typically stick to else if
unless I am using short tags. Is this correct??
They both have the exact same behavior and either can be used unless you not using curly brackets, then you should be using just elseif
There is this though:
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
When using a colon instead of curly brackets, that's what I call short tags, so I was already right on that one.
I was just reading up somewhere that elseif is slightly faster than else if????
Ah, I assumed you were talking about <?
when you said short tags. What you are actually talking about is PHP short hand.
I have never heard of one being faster than the other though.
From php manual
In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
Similar question exist in http://stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous . Though i am nt sure about the answer there as one of them suggest else if as else { if(){}}
I always assumed they were the same, thanks for the heads up on the colon format though (+1 guys) - not that I'd ever use such an ugly form :)
I use the colon format for all my PHP templates!! Curly braces in templates just doesn't make sense to me. Here's an example of my PHP templates:
<div id="foo">
<h1>Heading</h1>
<?php if ($bar == 5): ?>
<div class="bar">Five!</div>
<?php else: ?>
<div class="baz"><?= $bar ?></div>
<?php endif; ?>
<div>My name is <?= $name ?></div>
</div>
Like I said ugly. :)
I don't think any php conditional form in a template can be made to look nice. The closest I get to the colon form is twig's templater (my fave templater at the mo):
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
http://twig.sensiolabs.org/doc/tags/if.html
I also see that you're using short tags (<?=) - makes sense for 5.4+ - I will be adopting this too. Had a few icky moments a couple of years back when a cheap (read: useless) host would not allow short tags and I had to do a full text replace.
Sorry, but I'm not seeing why you think your version looks all that more impressive and clean than
<?php if (kenny.sick): ?>
Kenny is sick.
<?php elseif (kenny.dead): ?>
You killed Kenny! You #######!!!
<?php else: ?>
Kenny looks okay --- so far
<?php endif; ?>
Sorry, but I'm not seeing why you think your version looks all that more impressive and clean than
Now, now Dani, play nice. I never said I thought that 'my' version (re: twig) looks 'more impressive and clean'. I think I said that they were all ugly. Anyway, it's a personal thing, I just don't like the colon syntax - it's ugly to me as is seems unfinished somehow. It may be down to my big-waisted trousers, but I like braces. :)
Oh, I took what you said to mean that colon form is ugly and your favorite alternative to combat that ugliness is twig's templater.
When it comes to coding style it is down to personal preference.
I too was once burned by the php short codes not being supported on an OLDER server and since have NEVER recovered!
I've seen the
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
syntax before and thought to myself WTF is that because I'd never seen it before in PHP. I have a similar aversion to ternary operators etc.
Perversely, I love terneries.
Similar question exist in http://stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous . Though i am nt sure about the answer there as one of them suggest else if as else { if(){}}
Sorry to have to say this, but we all know about ‘stack overflow’; thank you!
We are not lazy nor ignorant… we're just members of DaniWeb, and that's why we are hanging here @ http://www.daniweb.com/ while reading your post, not there!
and I really forgot when was the last time I've used the "else if" || "elseif"; syntax.
As iamthwee stated coding style is a personal choice. But this is actually above coding style, it is in the core of programming logic if you choose this alternative syntax for control structure.
Most languages are architectural neutral, I tend to believe (with no so much joke on it) that more over PHP is programming paradigm neutral. You can write object oriented PHP or functional PHP or procedural PHP or any mix of them together, and of course you can use things like GOTO and sections , trails or this alternative syntax (IF ELSEIF ENDIF).
I will not take any further position on this, just to state that to decide things like that (in any language) need a bit caution because the programming culture that you will build will follow you.
"elseif" and "else if" are has the same output.
I was just reading up somewhere that elseif is slightly faster than else if????
I don't have experience writing programming languages, but from a logical standpoint this sounds like it could make sense. Since white space separates tokens, elseif
is just one token the language needs to process, and should just execute the behavior one would expect of it. This is as opposed to else if
being two separate tokens, each with its own behavior. I can't imagine the difference being noticeable in a practical sense, though. Did the piece you read mention how many else if
s you would have to run through before you noticed the change in speed?
But I'm with diafol on this one -- I started programming with C++ and have always been loyal to using {}'s when possible. And I love using the ternary operator, for whatever dumb reason ^.^
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.