isset() is faster than using
strlen() because calling a function is more expensive than using a language construct.
isset() method is consistently about 6 times faster.
Test to show the speed http://codepad.org/ztYF0bE3
strlen() over 1000000 iterations 7.5193998813629 isset() over 1000000 iterations 0.29940009117126
Top comments (3)
I think the explanation is a bit short, and that beginners could easily make mistakes.
issetdo not have the same functionality:
strlenallows to test the length of a string.
issetallows you to test that variable is defined and not
You really need to have a specific performance need to use what is described in the article (ie iterate 1,000,000 times).
In most cases, I prefer to use
strlen: if your business need is for example that your password has a minimum length of 8 characters, then the translation in the code will be much more explicit than with
If you want to test that a string is not empty, it is better I think to do
Thanks @Jimmy Klein
Thanks @Jimmy Klein very much