To find the length of a string

Oct 01, 2009 Author: makedon

You may find that sometimes you need the length of a string. This is helpfulif you want to get the last character of a string or check to make sure astring isn’t too long to fit somewhere (such as a particularly limited placeon a Web page or in a size-limited database field).To find the length of a string, use the strlen function, which has the followingsyntax:

int strlen(string str)
To find the length of a string $str, then, you would use
echo strlen($str);
The complete number of characters (including whitespace characters suchas spaces and \n) is returned. Here’s an example:
$str = ‘This is a string.’;
$str2 = 'Newlines!\nOne\nTwo';
echo strlen($str) . ','. strlen($str2);
The output of this code would be 17, 17. Remember that even though the second string appears longer, the \n sequence inside of double quotes is interpreted as only one character. Thus, the two strings are of equal length.

If you have something to ask about this tutorial. Please fell free to write a comment.

views 4591
  1. Add New Comment