Signup/Sign In
LAST UPDATED: AUGUST 4, 2021

How to find string length in PHP?

Answer: Using strlen() function

The strlen() function is the in-built string function in PHP. This function is used to find the length of the string. The length simply denotes the number of characters, special characters, and whitespace within the string.

This function enables us to calculate the length of the given string, including all whitespace and special characters. It will return 0 (zero) if the string is empty.

Syntax

strlen(string)

Example: Find String length in Php

In the given example, we have calculated the length of the string "Studytonight" using strlen() function.

<!DOCTYPE html>
<html>
<head>
	<title>string length in PHP</title>
</head>
<body>
	<?php
	$str = "Studytonight"; 
	echo strlen($str);
	?>
</body>
</html>

Output


12

Example 2: Find String length in Php

The result of strlen() can be appended with a string by using the dot(.) operator.

<!DOCTYPE html>
<html>
<head>
	<title>string length in PHP</title>
</head>
<body>
	<?php
	$str = "Welcome to Studytonight"; 
	echo "The string length is " .strlen($str);
	?>
</body>
</html>

Output


The string length is 23

Conclusion

In this lesson, we have explained how to find the length of the string in PHP. So, in PHP, we can find the length of the string using the strlen() function. This function calculates the length of the string, including whitespaces and special characters.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.