Use isset() Instead of strlen()

by Sanju 2010-02-01 11:21:43

Use isset() Instead of strlen()

When you treat strings as arrays, each character in the string is an element in the array. By determining whether a particular element exists, you can determine whether the string is at least that many characters long.

<?php
if (isset($username[5])) {
// The username is at least six characters long.
}?>


Note that the first character is element 0, so $username[5] is the sixth character in $username.

The reason this is slightly faster than strlen() is complicated. The strlen() is a function, and isset() is a language construct.
Generally speaking, calling a function is more expensive than using a language construct.

Tagged in:

840
like
0
dislike
0
mail
flag

You must LOGIN to add comments