Saturday, September 6, 2025
HomeLanguagesHow to get String Length in PHP ?

How to get String Length in PHP ?

In this article, we learn how to find the length of the string in PHP. 

Approach: This task can be done by using the built-in function strlen() in PHP. This method is used to return the length of the string. It returns a numeric value that represents the length of the given string. 

Syntax:

strlen($string)

Example 1: 

PHP




<?php
// PHP program to count all
// characters in a string
$str = "neveropenlover";
      
// Using strlen() function to
// get the length of string
$len = strlen($str);
  
// Printing the result
echo $len;
?>


Output:

18

Example 2: Below code demonstrates the use of strlen() function in PHP 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 LuckyGeeks ;";
      
// Here '\n' has been counted as 1
echo strlen($str);
?>


Output:

14
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS