Thursday, September 4, 2025
HomeLanguagesPHP strlen() Function

PHP strlen() Function

In this article, we will see how to get the length of the string using strlen() function in PHP, along with understanding its implementation through the examples.

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters. 

Syntax:

strlen($string);

Parameters: The strlen() function accepts only one parameter $string which is mandatory. This parameter represents the string whose length is needed to be returned. 

Return Value: The function returns the length of the $string including all the whitespaces and special characters. 

Below programs illustrate the strlen() function in PHP:

Example 1: The below example demonstrates the use of the strlen() function in PHP.

PHP




<?php
 
    // PHP program to find the
    // length of a given string
    $str = "neveropen";
     
    // prints the length of the string
    // including the space
    echo strlen($str);
?>


Output:

13

Example 2: This example demonstrates the use of the strlen() function where the string has special characters and escape sequences.

PHP




<?php
 
    // PHP program to find the
    // length of a given string which has
    // special characters
    $str = "\n neveropen Learning;";
     
    // here '\n' has been counted as 1
    echo strlen($str);
?>


Output:

25

Reference: http://php.net/manual/en/function.strlen.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS