Skip to content
Related Articles

Related Articles

How to get String Length in PHP ?

View Discussion
Improve Article
Save Article
  • Last Updated : 20 May, 2021

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. 



Example 1: 


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



Example 2: Below code demonstrates the use of strlen() function in PHP where the string has special characters and escape sequences.


// 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);


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!