Understanding PHP strlen() Function: Syntax, Parameters, Return Value, and Examples
This article explains the PHP strlen() function, covering its basic syntax, required parameter, return value, and provides two code examples that demonstrate how to obtain the length of regular strings and strings containing special characters or escape sequences.
Basic Syntax
<code>strlen($string)</code>Parameters: strlen() accepts a single required argument $string , which is the string whose length you want to measure.
Return Value: strlen() returns the length of the given string ($string) , counting all spaces and special characters.
Below are examples demonstrating the usage of the strlen() function.
Example 1:
<code><?php
$str = "hello PHP";
// Output the length of the string
echo strlen($str);
?></code>Output:
9
Example 2: Output length of a string containing special characters and escape sequences
<code><?php
$str = "\n chetna ;";
// Output the length of the string, which includes special characters and escape sequences
echo strlen($str);
?></code>Output:
10
Note: The escape sequence '\n' is counted as a single character.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.