vineld Posted July 27, 2009 Share Posted July 27, 2009 I have not really began using the short-hand if statements with question marks and colons myself, mostly because I think it makes the code more difficult to read for others, but I am wondering why those of you who use it have chosen to do so? Is it just to shorten the code? Performance-wise, which alternative is best, if any? Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/ Share on other sites More sharing options...
.josh Posted July 27, 2009 Share Posted July 27, 2009 It's called a ternary operator. I do not like using it for complex conditions (especially disagree with nesting them) for non-readability purposes. But for simple things like assignment of something vs. a default, or as a simple switch, I'm all for it. Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/#findComment-884325 Share on other sites More sharing options...
vineld Posted July 28, 2009 Author Share Posted July 28, 2009 I think that is a sound way of thinking. Thanks for correcting me as well, English is not my mother tongue so I may seem a big ignorant at times Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/#findComment-884801 Share on other sites More sharing options...
Mark Baker Posted July 28, 2009 Share Posted July 28, 2009 Performance-wise, which alternative is best, if any? I ran some tests a while back, and found it was fractionally slower using the ternary operator than writing out the full if statement in longhand. Quite why this should be, I don't know.... I'd expected to find that both forms compiled to the same code. Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/#findComment-884815 Share on other sites More sharing options...
patrickmvi Posted July 28, 2009 Share Posted July 28, 2009 I absolutely HATE the shorthand if statements. In my opinion they make things so much harder to read and digest when you're trying to debug something. However, I can't speak to whether or not they are more or less efficient. I would think they would be more efficient but I can't say for sure. Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/#findComment-884833 Share on other sites More sharing options...
vineld Posted July 28, 2009 Author Share Posted July 28, 2009 Performance-wise, which alternative is best, if any? I ran some tests a while back, and found it was fractionally slower using the ternary operator than writing out the full if statement in longhand. Quite why this should be, I don't know.... I'd expected to find that both forms compiled to the same code. Thanks for the input, fractionally slower would mean that it doesn't really matter which one you use then. Even if they compile to the same code they will still need to be parsed and that's probably wherein the difference lies. Quote Link to comment https://forums.phpfreaks.com/topic/167680-short-hand-if-statements-better-or-not/#findComment-884919 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.