Concept

How can you determine the length of a PHP string?

ComputerScienceOne / String Functions

"PHP provides dozens of convenient functions that allow you to process and modify strings. We highlight a few of the more common ones here. A full list of supported functions can be found in standard documentation. Because of the history of PHP, many of the same functions defined in the C string library can also be used in PHP.\n\nWhen accessing individual characters in a string, it is necessary that we know the length of the string so that we do not access invalid characters (though doing so is not an error, it just results in null). The strlen() function returns an integer that represents the number of characters in the string.\n\n1 $s = \"Hello World!\";\n\n2 $x = strlen($s); //x is 12\n\n3 $s = \"\";\n\n4 $x = strlen($s); //x is 0\n\n5\n\n6 //careful:\n\n7 $s = NULL\n\n8 $x = strlen($s); //x is 0\n\nAs demonstrated in the last example, strlen() will return 0 for null strings. Recall that we can distinguish between these two situations by using is_null(). Using this function we can iterate over each individual character in a string.\n\n1 $fullName = \"Tom Waits\";\n\n2 for($i=0; $i<strlen($fullName); $i++) {\n\n3 printf(\"fullName[%d] = %s\n\n\", $i, $fullName[$i]);\n\n4 }"

Related Ideas

How can you determine the length of a PHP string? | ComputerScienceOne | Bifalgorithm | Bifalgorithm