PHP | strlen() Function

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() fucntion accepts only one parameter $string which is mandatory. This parameter represents the string whose length is needed to be returned.



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

Examples:

Input : "abc"
Output : 3

Input : "\n chetna ;"
Output : 10
Explanation : '\n' is considered as a single character
              as it is an escape sequence.

Input : "geeks for geeks" 
Output :15

Below programs illustrate the strlen() function in PHP:

Program 1: The below program demonstrates the use of strlen() function in PHP.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

15

Program 2: The below program demonstrates the use of strlen() function in PHP where the string has special characters and escape sequences.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

10

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.