Why not implement whatever rule you want directly in PHP?
Alternatively, if you want to ban a robot, why not just get apache to deny access based on this rule? Then the PHP won't even need to be executed.
Something like
SetEnvIf User-Agent nastybot go_away
Order Allow, deny
Allow from all
Deny from env=go_away
Mark