jmartinsen Posted October 27, 2012 Share Posted October 27, 2012 Hi all. What's the best practice with using the Ternary Operator vs. the Error Control Operator when setting a simple varaible? I doubt there is any performance issues? <?php $var = isset($_GET["var"]) ? $_GET["var"] : ""; $var = @$_GET["var"]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/ Share on other sites More sharing options...
trq Posted October 27, 2012 Share Posted October 27, 2012 You doubt there is any performance issues? When PHP generates an error there are costs involved. Simply suppressing the error does not eliminate all of these costs. Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1388102 Share on other sites More sharing options...
jmartinsen Posted October 27, 2012 Author Share Posted October 27, 2012 Oh, you're absolutely right. I first thought that the @ ignored the error handler altogether, but it doesnt. It calls it, but just doesnt do anything with it. I did a supersimple test and it shows that the ternary operator was 4 times faster. With performance out of the picture any thoughts on using one over the other? Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1388108 Share on other sites More sharing options...
Pikachu2000 Posted October 27, 2012 Share Posted October 27, 2012 Suppressing errors makes debugging code exponentially more difficult. Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1388114 Share on other sites More sharing options...
Jessica Posted October 27, 2012 Share Posted October 27, 2012 Plus the suppression way gives a different result than the ternary, in the event that its not set. Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1388116 Share on other sites More sharing options...
salathe Posted October 27, 2012 Share Posted October 27, 2012 the suppression way gives a different result than the ternary This is the important point, more so than any performance concerns of the @-operator. The ternary example allows you to define the value when the variable is not set, whereas the @'d variable will give you NULL (so you'll later go check for NULL and assign a value, right). Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1388120 Share on other sites More sharing options...
shlumph Posted November 20, 2012 Share Posted November 20, 2012 Also, from a readability standpoint, the @-operator makes me stop and think a little bit longer when debugging/skimming code. "Wait, what happens when..." Quote Link to comment https://forums.phpfreaks.com/topic/269975-setting-a-variable-ternary-operator-vs-error-control-operator/#findComment-1393846 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.