str length PHP | How to Get PHP String Length with Example?
Summary: In this article, you will learn, what is str length PHP and How to Get PHP String Length with Example, Let’s understand in details. I hope you will enjoy it!.
Answer: Use strlen() and mb_strlen()
2. Use the PHP mb_strlen() Function to Measure String Length in Bytes in PHP.
In this article you will learn different methods to get the PHP string size in bytes.
1. Use the PHP strlen() Function to Measure String Length in Bytes in PHP
The strlen() function is predefined function of PHP. We will use PHP’s built-in function strlen() to get the string length in bytes. It is used to find the length of the string or returns the length of a string including all the whitespaces and special characters. It is a specialized function to get the string length.
Syntax:
The syntax to use this function is as follows.
Parameter Value
This function has one parameter only. the detail of its parameter is as follows.
| Variables | Description | 
|---|---|
| $string | It is the string whose length will be returned by the function and It is a mandatory parameter. | 
This function returns the length of the string. furthermore, the program below shows how we can use the strlen() function to measure or get the length of a PHP string in bytes.
Program:1
| 1 2 3 4 5 | <?php $mystring = "Knowledge is power."; echo("The string length in bytes is: "); echo(strlen($mystring)); ?> | 
The function has returned the string length in bytes.
Program:2
| 1 2 3 4 5 6 | <?php   $mystring = 'Welcome To Tutorialscan';   echo "Your String is:".$mystring;   echo "<br>";   echo "By using 'strlen()' function:".strlen($mystring); ?> | 
By using ‘strlen()’ function:23
The function has returned the string length in bytes.
2. Use the mb_strlen() Function to Measure String Length in Bytes in PHP
The mb_strlen() function is predefined function of PHP. We can use PHP’s built-in function mb_strlen() to get the string length in bytes. It is a specialized function to get the string length but it is less efficient than the PHP strlen() function.
Syntax:
The syntax to use this function is as follows.
Parameter Value
This function has two parameters. The detail of its parameter is as follows
| Variables | Description | 
|---|---|
| $string | It is an amendatory parameter, this is a string whose length will be returned by the function. | 
| $encoding | It is the encoding we’ve used for our string because as a result, different encoding schemes have different sizes. | 
This function returns the length of the string. The program below shows how we can use the mb_strlen() function to get the PHP string length.
Program:1
| 1 2 3 4 5 | <?php $mystring = "Welcome To Tutorialscan"; echo("The string length in bytes is: "); echo(mb_strlen($mystring)); ?> | 
The function has returned the measured string length in bytes.
Program:2
| 1 2 3 4 5 6 | <?php   $mystring = 'Welcome To Tutorialscan';   echo "Your String is:".$mystring;   echo "<br>";   echo "By using 'mb_strlen()' function:".mb_strlen($mystring); ?> | 
By using ‘mb_strlen()’ function:23
The function has returned the measured string length in bytes.
str length PHP: Conclusion
In this article, you have learned what is str length PHP and How to Get PHP String Length with Example. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!
