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 do you find the length of a string and iterate over its characters?ComputerScienceOne · String Methods
- How can strlen determine the length of a C string?ComputerScienceOne · String Library
- How does PHP handle undefined variables, null values, and constants?ComputerScienceOne · Using Variables
- How can individual characters in a string be accessed safely?ComputerScienceOne · Basic Operations
- How can you compute a substring in PHP?ComputerScienceOne · String Functions
- What are PHP strings and how should they be treated?ComputerScienceOne · Basics
- How are PHP strings copied and modified by character?ComputerScienceOne · Basics
- How can you extract substrings from a string in PHP?ComputerScienceOne · Arrays of Strings