Portal Home > Knowledgebase > Articles Database > strpos() Empty delimiter in...
strpos() Empty delimiter in...
|Posted by gpl24, 05-02-2011, 12:40 AM|
|I tested this with a blank user agent and can't figure out why the code is logging errors. Any tips?
|Posted by WootWoot, 05-02-2011, 01:27 AM|
|The error log is self-explanatory : if strpos' delimiter is empty... boom.
|Posted by gpl24, 05-02-2011, 02:15 AM|
|Crap.. really that easy?
Embarrassing, to say the least.
|Posted by sea otter, 05-03-2011, 02:01 AM|
I noticed an error which could lead to missing results, and some minor tweaks you could make for greater efficiency; they're small, but it all adds up, especially with PHP.
$agent is loop invariant (never changes) so to be efficient we can move it outside the for() loop and only check it once.in your case here, instead of !empty($agent) you can simply test $agent.since you're only testing to see if there is a bot present (and not which/how many), for greater efficiency you can break out of the loop as soon as you find one. Why waste time and cpu cycles once you already have your answer?Here's the error: if $bot is at the very start of $agent, strpos() will return 0 (zero). The way you test strpos() means that you will miss these cases. To catch them, you must explicitly test strpos() against FALSE.
Also, if you know which bots visit you the most and you control the mechanism by which $arr is populated, order the array with the most-visited bots listed first; over the long run you'll traverse $arr faster that way.
Hope that helps
|Posted by gpl24, 05-03-2011, 11:15 PM|
|sea otter, thanks for the tip. I'm definitely trying your code out; just plugged it in the script!
Add to Favourites Print this Article
Arbor System (Views: 297)