Open In App

PHP strlen() Function

Last Updated : 05 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The strlen() is a built-in function in PHP which returns the length of a given string.

It calculates the length of the string including all the whitespaces and special characters. 

Syntax:

strlen($string);

Parameters:

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. 

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 = "GeeksforGeeks";

// 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 GeeksforGeeks Learning;";

// here '\n' has been counted as 1
echo strlen($str);

?>

Output:

25

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.


Next Article

Similar Reads