Backend Development 2 min read

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

<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>&lt;?php
    $str = "hello PHP";
    // Output the length of the string
    echo strlen($str);
?&gt;</code>

Output:

9

Example 2: Output length of a string containing special characters and escape sequences

<code>&lt;?php
    $str = "\n chetna ;";
    // Output the length of the string, which includes special characters and escape sequences
    echo strlen($str);
?&gt;</code>

Output:

10

Note: The escape sequence '\n' is counted as a single character.

backend developmentphpCode Examplestring 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

login 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.