Jump to content

Recommended Posts

This is going to sound perhaps a little pointless, but...

 

Is there any difference between:

if (something) {

 

  ...

 

} elseif {

 

  ...

 

}

 

And...

if (something) {

 

  ...

 

} else if {

 

  ...

 

}

 

Note the elseif verse else if.

 

Both work, is there a difference in execution time? Is one considered more "proper" than the other?

Link to comment
https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/
Share on other sites

http://php.net/manual/en/control-structures.elseif.php

 

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.

 

 

Please note that in my example, I *neglected* to include the if conditions for the elseif / else if, but of course it was an oversight...

 

Now, I guess the reason I ask about execution times is that if one verses the other executed some division of milliseconds faster because for example it required less interpretation by PHP, for a very large application or one that got a lot hits, it could become a matter of efficiency to eliminate all the millisecond slower methods / structures / functions...

 

After all, one wants to be wickedly fast, right?

PHP ignores space unless it is inside a string.
Even things like if(strlen(trim($quickdesc))==0){$error_flag=1;$error_text[9]="<br />Please enter a Short Description.";}?

 

Can you use such techniques to compact your PHP code? Would it load (and thus "run") faster from the user perspective?

That is valid syntax.  Now if it's faster or not, I doubt you would notice either way.

Well, white space removal is one of the techniques that JS compactors use. I assume it makes it run faster. Although obviously running code on the server is different than running code in the browser...

 

Anyway thanks for your comments!

Compact to me means shrinking the file size.

Of course. And this benefits JS because a smaller file makes it to the browser quicker.

 

So you are telling me there is no benefit in server side scripts by removing white space (other than less storage space used)?

 

Anyway, I'm really more interested in my original question about elseif.

 

Nevermind...

You shouldn't worry about micro-optimization because it won't help you if:

 

1) you have a query on a big table with wrong indexes

2) you used the wrong engine for your table so it locks up every time someone writes to it

..

 

Analyze your code under load, find bottlenecks and solve them!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.