Arty Ziff Posted November 27, 2010 Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/ Share on other sites More sharing options...
jcbones Posted November 27, 2010 Share Posted November 27, 2010 PHP ignores space unless it is inside a string. Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140201 Share on other sites More sharing options...
revraz Posted November 27, 2010 Share Posted November 27, 2010 That is not true. You can't do i f (1=1) echo "HI"; Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140204 Share on other sites More sharing options...
revraz Posted November 27, 2010 Share Posted November 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140205 Share on other sites More sharing options...
Arty Ziff Posted November 27, 2010 Author Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140216 Share on other sites More sharing options...
revraz Posted November 27, 2010 Share Posted November 27, 2010 That is valid syntax. Now if it's faster or not, I doubt you would notice either way. Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140219 Share on other sites More sharing options...
Arty Ziff Posted November 27, 2010 Author Share Posted November 27, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140222 Share on other sites More sharing options...
revraz Posted November 27, 2010 Share Posted November 27, 2010 Compact to me means shrinking the file size. Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140223 Share on other sites More sharing options...
Arty Ziff Posted November 27, 2010 Author Share Posted November 27, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140227 Share on other sites More sharing options...
revraz Posted November 27, 2010 Share Posted November 27, 2010 No, I'm saying the difference is probably not even noticed. And the original question was answered in the Link I provided. Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140252 Share on other sites More sharing options...
ignace Posted November 28, 2010 Share Posted November 28, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/219961-are-these-the-same-of-different/#findComment-1140552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.