Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Get the length of a string in PHP by using strlen function

To find out an estimate of the string’s length within PHP you can make use of the strlen() function. The string is passed as an the argument for strlen() and the function returns an integer that represents the length of the string.

Syntax:

strlen($string)

Parameters Strlen() function can only accept one parameter, $string, which is required. The string parameter is the one that must be the length in order to return it.

Return Value: Function returns length for the string including all whitespaces and special characters.

Example – String Length

In the next example, we will use the following PHP String literal and determine its length by using the strlen() method.

PHP Program-1

$str = ‘Hello Devapoint’;

“$length” = strlen($str);

echo $length;

?>

Program Output 16

Use to use the PHP strlen() function

It is easy to make use of to use the PHP strlen() function to determine the length of the string. strlen() function will return an amount of length upon the success of the test, and returns 0 when the string is empty.

Let’s look at the following illustration to better understand how it all works:

Example: See this code>

$str1 = “Hello PHP!’;

echo strlen($str1);

// Outputs: 10

The following program illustrates the use of strlen() function in PHP where the string contains particular characters, escape sequences and special characters.

PHP program to determine the length of a string, which contains Special characters

$str = “\t Devapoint;”;

// here “n and;” is counted as 1. So

echo strlen($str);

?>

Output: 12

Program 2

The following program shows the usage of the strlen() function in PHP.

PHP program to determine the

/length of a particular string

$str = “Deva Point”;

// Prints the length including the white space also

echo strlen($str);

?>

Output: 10

Published
Categorized as PHP

Leave a comment

Your email address will not be published.