How To Find The Length Of A String In PHP With Example

Finding the length of a string in PHP is very simple we just need to use the strlen() function.

In this article, we will talk about on how to find the length of a string in the easiest way as well as example program that will help you to understand and learn. This topic is a continuation of the previous topic, entitled PHP Exit Function.

How do I find the length of a string in PHP?

In PHP, strlen() is a built in function which find the length of a string. Further, the function takes the given string as a parameter and returns the length of the string.

The PHP strlen() function returns the length of a string in PHP and calculates the length of the given string which includes all the special characters and whitespaces.

Syntax

strlen($string);

Example

<?php
$stringOne = 'This is a testing to find the length strlen of a string in PHP!';
echo strlen($stringOne)."\n"; 
 
$stringTwo = 'This    is a testing to find    the length of a mb strlen string in PHP!';
echo strlen($stringTwo); 
?>

Output

63
72

What is the max length of string in PHP?

The maximum length of a string is always 2gb and the length always have 32 bits and bit is wasted because in most cases int is always use rather than string.

Further, int is impractical for having a length of over 2gb as it only requires a cast to keep away from breaking arithmetic or (than) comparison.

What is the max size of a string?

The maximum size of a string is 2048 number of bytes and cannot be longer and a literal string roughly 65535 bytes and can be constructed by just concatenating the strings.

How do I count characters in a string in PHP?

The substr_count() is a function which can count the number of characters into a string and returns the number of a substring that occurs in substring, once the string is empty it will produce a null value.

Summary

In summary, you have learned about on how to find the Length Of A String In PHP. This article also discussed what is the max length of string, what is the max size of a string, and how do I count characters in a string.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment