View Single Post
Old 12-12-2007, 03:26 PM   #4 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Quote:
Originally Posted by 40 tips
When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.

Ex.
PHP Code:
if (strlen($foo) < 5) { echo "Foo is too short"; } 
vs.
PHP Code:
if (!isset($foo{5})) { echo "Foo is too short"; } 
I knew it!
Jay is offline  
Reply With Quote