note 83171 added to function.strlen

From: Date: Tue, 13 May 2008 21:12:02 +0000
Subject: note 83171 added to function.strlen
Groups: php.notes 
Request: Send a blank email to php-notes+get-139363@lists.php.net to get a copy of this message
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(); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 67.163.248.210 ---- Manual Page -- http://www.php.net/manual/en/function.strlen.php Edit -- https://master.php.net/note/edit/83171 Del: integrated -- https://master.php.net/note/delete/83171/integrated Del: useless -- https://master.php.net/note/delete/83171/useless Del: bad code -- https://master.php.net/note/delete/83171/bad+code Del: spam -- https://master.php.net/note/delete/83171/spam Del: non-english -- https://master.php.net/note/delete/83171/non-english Del: in docs -- https://master.php.net/note/delete/83171/in+docs Del: other reasons-- https://master.php.net/note/delete/83171 Reject -- https://master.php.net/note/reject/83171 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139363) next »