View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we will see how to get the length of the string using strlen[] function in PHP, along with understanding its implementation through the examples.
The strlen[] is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.
Syntax:
strlen[$string];
Parameters: The strlen[] function accepts only one parameter $string which is mandatory. This parameter represents the string whose length is needed to be returned.
Return Value: The function returns the length of the $string including all the whitespaces and special characters.
Below programs illustrate the strlen[] function in PHP:
Example 1: The below example demonstrates the use of the strlen[] function in PHP.
PHP
Output:
13
Example 2: This example demonstrates the use of the strlen[] function where the string has special characters and escape sequences.
PHP
Output:
25
Reference: //php.net/manual/en/function.strlen.php
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
The strlen[] function is used to get the string length. It returns the length of the string on success. If the string is empty, 0 is returned.
Syntax
strlen[str]
Parameters
str − The string for which we want the length.
Return
The strlen[] function returns the length of the string on success. If the string is empty, 0 is returned.
Example
The following is an example −
Live Demo
Output
The following is the output −
10
Updated on 26-Dec-2019 09:24:59
- Related Questions & Answers
- strlen[] php function giving the wrong length of unicode characters ?
- What is strlen function in C language?
- Write a C program demonstrating strlen library function
- file_exists[] function in PHP
- basename[ ] function in PHP
- chgrp[]function in PHP
- chmod[] function in PHP
- chown[] function in PHP
- clearstatcache[] function in PHP
- copy[] function in PHP
- delete[] function in PHP
- dirname[]function in PHP
- disk_free_space[] function in PHP
- disk_total_space[] function in PHP
- diskfreespace[] function in PHP
[PHP 4, PHP 5, PHP 7, PHP 8]
strlen — Get string length
Description
strlen[string $string
]: int
Parameters
string
The string being measured for length.
Return Values
The length of the string
on success, and 0
if the string
is empty.
Examples
Example #1 A strlen[] example
Notes
Note:
strlen[] returns the number of bytes rather than the number of characters in a string.
Note:
strlen[] returns
null
when executed on arrays, and anE_WARNING
level error is emitted.
See Also
- count[] - Counts all elements in an array or in a Countable object
- grapheme_strlen[] - Get string length in grapheme units
- iconv_strlen[] - Returns the character count of string
- mb_strlen[] - Get string length
rm dot nasir at hotmail dot com ¶
6 years ago
I want to share something seriously important for newbies or beginners of PHP who plays with strings of UTF8 encoded characters or the languages like: Arabic, Persian, Pashto, Dari, Chinese [simplified], Chinese [traditional], Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, and etc.
As the manual says: "strlen[] returns the number of bytes rather than the number of characters in a string.", so if you want to get the number of characters in a string of UTF8 so use mb_strlen[] instead of strlen[].
Example:
chernyshevsky at hotmail dot com ¶
18 years ago
The easiest way to determine the character count of a UTF8 string is to pass the text through utf8_decode[] first:
utf8_decode[] converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
joeri at sebrechts dot net ¶
6 years ago
When checking for length to make sure a value will fit in a database field, be mindful of using the right function.
There are three possible situations:
1. Most likely case: the database column is UTF-8 with a length defined in unicode code points [e.g. mysql varchar[200] for a utf-8 database].
2. The database column has a length defined in bytes [e.g. oracle's VARCHAR2[200 BYTE]]
3. The database column is in another character set [UTF-16, ISO-8859-1, etc...] with a length defined in characters / code points.
Find the character set used, and pass it explicitly to the length function.
jasonrohrer at fastmail dot fm ¶
9 years ago
PHP's strlen function behaves differently than the C strlen function in terms of its handling of null bytes ['\0'].
In PHP, a null byte in a string does NOT count as the end of the string, and any null bytes are included in the length of the string.
For example, in PHP:
strlen[ "te\0st" ] = 5
In C, the same call would return 2.
Thus, PHP's strlen function can be used to find the number of bytes in a binary string [for example, binary data returned by base64_decode].
vcardillo at gmail dot com ¶
10 years ago
I would like to demonstrate that you need more than just this function in order to truly test for an empty string. The reason being that will return 0. So how do you know if the value was null, or truly an empty string?
Null length is Zero !is_null[]: $foo is probably null !is_null[]: $bar is truly an empty string So it would seem you need either is_null[] or isset[] in addition to strlen[] if you care whether or not the original value was null.
// Begin Output:
Length: 0
Length: 0
Length: 0
Null length is still Zero
isset[]: $foo is probably null
isset[]: $bar is truly an empty string
// End Output
basil at gohar dot us ¶
12 years ago
We just ran into what we thought was a bug but turned out to be a documented difference in behavior between PHP 5.2 & 5.3. Take the following code example: