note 83171 deleted from function.strlen by derick
| From: | derick@php.net | Date: | Sun, 21 Sep 2008 20:10:24 +0000 |
| Subject: | note 83171 deleted from function.strlen by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144590@lists.php.net to get a copy of this message | ||
Note Submitter: sedativchunk at gmail dot com
----
When checking for the length of a string, it is faster NOT to use the strlen function (that is, if
you do not need to print how many characters are in a string). If you are using this for checking
inputted information for lengths to big, you should use isset instead. Example:
<?php
//Faster method for only checking string length
/** Lets say we are collecting a submitted username for a registration form and we want to see if
the username is over 30 characters. It is faster NOT to use strlen when checking for the length if
you do not need to print the number of characters in a string. **/
//collect post information
$username = $_POST['username'];
//is the username more than 30 characters?
//simply use the isset function and curly brackets after var
if(isset($username{31})) {
echo '<b>error: your username cannot be more than (30) characters long.</b><br
/>';
} else {
echo 'Your username is the proper length!<br />';
}
exit();
?>