If you were asked to check up on the length of string, you’d probably use the following code:
$str = "This is a string"; echo strlen($str);
Let’s look at a different way of doing this:
$str = 'This is a string'; if (isset($str[9])) { echo 'The input is longer or equal then 10 characters.'; } else { echo 'The input is less then 10 characters long.'; }
The equivalent with strlen:
$str = 'This is a string'; if (strlen($str) >= 10) { echo 'The input is longer or equal then 10 characters.'; } else { echo 'The input is less then 10 characters long.'; }
Now, why use isset instead of strlen? I’ve read documents claiming that isset runs up to 5 time faster than strlen in most cases. But that is not the main reason for wanting to use it. Try using strlen on a variable that has not been initialized yet. Yes, I know, that is what PHP and languages like it has done: allow people to use uninitialized variables. And although I believe it to be bad programming practice to not initial variables, it happens. Getting back to using strlen on an uninitialized variable. You will get a “Notice” error telling you that it is not yet initialized. Although not a major problem, it is an irritation seeing these notices if you have them enabled.
So, start using isset if you want a bit more performance or start remember to initialize variables and use strlen.

