This is truly a neat trick, though the previous article utterly fails to clarify it. Here is that the missing example:
<?php
if (isset($username[5])) {
// The username is at least six characters long.
}
?>
When you treat strings as arrays, every character within the string is a component within the array. By determinant whether or not a selected component exists, you'll confirm whether or not the string is a minimum of that several characters long. (Note that the primary character is component zero, therefore $username[5] is that the sixth character in $username.)
The reason this is often slightly quicker than strlen() is difficult. the easy clarification is that strlen() may be a operate, and isset() may be a language construct. usually speaking, business a operate is dearer than employing a language construct.
No comments:
Post a Comment