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 strlen($string) 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:
<?php
$str = "hello PHP";
// Output the length of the string
echo strlen($str);
?>Output:
9
Example 2: Output length of a string containing special characters and escape sequences
<?php
$str = "
chetna ;";
// Output the length of the string, which includes special characters and escape sequences
echo strlen($str);
?>Output:
10
Note: The escape sequence '\n' is counted as a single character.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
