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.

php Courses
php Courses
php Courses
Understanding PHP strlen() Function: Syntax, Parameters, Return Value, and Examples

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPstring lengthstrlen
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.