spidermann Posted September 29, 2009 Share Posted September 29, 2009 Hi guys. I'm just curious as to which of these techniques would be more efficient. They both work properly, so this is more of a question of how you guys prefer to handle this type of thing. First, an if statement: if($paged == 0 || $paged == "") { $paged = 1; } Or a ternary operator: $paged = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ); The first is less code, so I'm leaning that way, but I'll ultimately go with whatever is more efficient for the server. Thanks for any advice you may have. Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/ Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 I'd do: $paged = (empty($paged) || $paged == 0) ? 1 : $paged; Ultimately your normal if statement would be the fastest, but that shouldn't matter because even ran through 1,000,000 times the difference in negligible (around 0.01 - 0.1 second(s)). The most important thing in cases like this is that it's easiest to read. Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/#findComment-927262 Share on other sites More sharing options...
spidermann Posted September 29, 2009 Author Share Posted September 29, 2009 Great, thanks very much. By the way, is there a tool needed to check the execution time like you just did there? Or is there a built-in way to see that info? Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/#findComment-927271 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 Just use microtime() For this specific case I did this: <?php $start = microtime(true); for($i = 0;$i < 1000000;$i++) if($paged == 0 || $paged == "") { $paged = 1; } echo (microtime(true) - $start); Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/#findComment-927273 Share on other sites More sharing options...
spidermann Posted September 29, 2009 Author Share Posted September 29, 2009 Sweet. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/#findComment-927278 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 No problem, just remember to press the "Topic Solved" button at the bottom left of the topic, and in the future. Quote Link to comment https://forums.phpfreaks.com/topic/175975-solved-which-is-more-efficient/#findComment-927280 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.