PHP String is one of the simplest data types having a sequence of characters. The length of a PHP string tells how many characters are there in the string. But we can also find the length of the string in bytes.
PHP offers a built-in function strlen( ) to find out the length of a specified string. This string function is used to return the length of the string in bytes. It does not return the number of characters in the output.
The article will demonstrate examples for using the strlen( ) function to find out the length string in bytes. There will be example code snippets along with output to have a proper understanding of the concept.
Table of Contents
Strlen( ) Function:
Strlen Function accepts a string in parameter, and in the output, it returns the size of string in bytes. The length is calculated, including special characters and white spaces. The function will return 0 if the string is empty. The syntax is given as:
Strlen Method is used for the validation of input in the form. If you want to ignore blank spaces in the string, you can use the string replace function before using a string length function.
Example Code: 01
<?php $mystring = "Learn with Code Leaks"; echo strlen($mystring); ?>
Example Code: 02
The string in this code contains special characters.
<?php $mystring = "\n Learn with @Code Leaks! ;"; echo ("The string length in bytes is: "); echo strlen($mystring); ?>
The article has discussed the strlen( ) function in detail with examples. I hope the example codes will help you understand how to find the length of the string in PHP.