Understanding the PHP String Length Function

phpstrstrLike all programming languages, ‘Text’ processing has a vital role in (PHP) Hypertext Preprocessor scripting language as well. One important module of ‘Text’ processing is the string and its functions. In PHP, a string is a series of characters, where each character is the same as a byte. Because of that, PHP only supports a 256 character set, therefore it does not support Unicode characters natively. PHP provides a set of functions for handling and manipulation of strings. Some of the important functions are ‘explode’ to split a string, ‘implode’ to join an array to a string, ‘trim’ to remove spaces from the string, ‘strstr’ to find first occurrence of string to replace, ‘substr_compare’ to compare two strings, ‘strtoupper’ to make an uppercase string, ‘strpos’ to find position of first occurrence of string and ‘strlen’ function to find a String’s ‘Length’.

New to PHP? Take a course at Udemy.com.

What is PHP String Length Function?

The PHP String Length function (‘strlen’) is used to find out the total length of characters in string. For example, it can be used to count the number of characters that is allowed in ‘TextField’ where data is further processed. To avoid the errors that may arise by using that data, the String ‘Length’ function notifies the user when a limit exceeds.

PHP String Length Function Syntax

int strlen (string $string)

As from the above format it is clear that String Length function ‘strlen’ returns an integer value. String is the argument whose characters count is to calculate.

How String Length Function Works?

The following code snippet demonstrates the use of String Length function to calculate the length of string literal.

Code snippet:

$characterCount = strlen ( 'Manipulation' );

echo $characterCount;

Output:

12

In the above code snippet, the String Length function ‘strlen’ calculates the length of characters in string literal and assigns it to a variable. Space is also considered a one plus in the length of a string. To use the method with string variable and string with spaces following code snippets can be used.

Code snippet 1:

$string = 'Manipulation';

$characterCount = strlen ( $string );

echo $characterCount;

Code snippet 2:

$string = 'Understanding the PHP String Length Function';

$characterCount = strlen ( $string );

echo $characterCount;

Output:

44

PHP String Length Function with UTF-8 Characters

The PHP String Length function ‘strlen’ returns the number of bytes rather than the total number of characters in the string. As this method count bytes and UTF-8 characters, it can be 4 bytes long, which is why ‘strlen’ with UTF-8 characters in string have different output than expected. The following code demonstrates the PHP String length function behavior with UTF-8 characters.

Code snippet:

$utfCharacters = 'êð';

$characterCount = strlen ( $utfCharacters );

echo $characterCount;

Output:

4

In the above output, the expected value to return from PHP String Length function is ‘2’ but instead of ‘2’ it returns ‘4’.

To learn more about PHP string functions, check out a course at Udemy.com.

PHP String Length different behaviors with ‘Array’

The PHP String Length function has different behaviors in different version of PHP. This change occurs with the release of PHP 5.3, which has a newer internal parameter parsing API. This parameter parsing API returns ‘null’ when invalid arguments are passed to a function.

Code snippet:

$currentTopic = array ( 'Understanding', 'PHP', 'String', 'Length', 'Function' );

if (strlen ( $currentTopic) == 0) {

echo "Null is return, so count is 0.";

}

else {

echo "Count of 5 is return. ";

}

Output of the above code snippet varies from the underlying PHP version on a machine. When a version of 5.3 or above is installed on a machine, the output will be a warning that ‘strlen ( ) expects parameter one to be string’ and ‘echo’ in the ‘if’ will print on screen. This is because of the new parameter parsing API; it returns ‘null’ with count of ‘0’ (discussed in next section) when ‘array’ is passed as an argument to PHP String Length function ‘strlen’, which only accepts the string as argument according to new version of documentation. In case of a PHP version 5.2 or below, the ‘else’ statements will executes because in absence of parameter parsing API, the String Length function returns the count of elements in ‘array’.

PHP String Length Function with ‘null’

Unlike most of all other programming language that use ‘Length’ of string to identify that it is empty or not, PHP String Length function ‘strlen’ cannot make that decision alone, because it also returns ‘0’ in case of ‘null’ value. To achieve this, PHP functions ‘is_null’ or ‘isset’ is used with ‘strlen’ having ‘AND’ operation between them.

Code Snippet:

$sampleString_01 = "";

$sampleString_02 =null;

echo 'Lenght of empty string is ' . strlen ( $sampleString_01 ) . '<br>';

echo 'Lenght of null string is ' . strlen ( $sampleString_02 ) . '<br>' ;

echo '<br>'; 

if (strlen ( $sampleString_01 ) == 0 && !is_null ( $sampleString_01 ) )

echo 'sampleString_01 is empty <br>';

if (strlen ( $sampleString_02 ) == 0 && is_null ( $sampleString_02 ) )

echo 'sampleString_02 is null <br>';

echo '<br>';

if (strlen ( $sampleString_01 ) == 0 && isset ( $sampleString_01 ) )

echo 'sampleString_01 is empty <br>';

if (strlen ( $sampleString_02 ) == 0 && !isset ( $sampleString_02 ) )

echo 'sampleString_02 is null <br>';

Output:

Lenght of empty string is 0

Lenght of null string is 0

sampleString_01 is empty

sampleString_02 is null

sampleString_01 is empty

sampleString_02 is null

In the above code output, it is clear that the String Length function returns a ‘0’ in both cases of empty and null string, thus ‘is_null’ and ‘isset’ functions are used to differentiate empty string from a null string.

PHP String Length Function alternative ‘mb_strlen’

mixed mb_strlen ( string $string [, string $encoding = mb_internal_encoding() ] )

An alternative to String Length function is ‘mb_strlen’ which supports the UTF-8 characters and add ‘1’ count for each UTF-8 character (multi-byte) unlike ‘strlen’. Accepts string and an encoding as arguments and returns number of characters in string or ‘false’ on invalid encoding parameter.

Code snippet:

$utfCharacters = 'êð';

$characterCount = mb_strlen ( $utfCharacters, "UTF-8" );

echo $characterCount;

Output:

2

The above code snippet string is the same as of String Length ‘strlen’ function for UTF-8 but the output in this case is different ‘2’ as compared to ‘4’ in ‘strlen’ case. This method can be achieved with an addition of one method ‘utf8_decode’ with String Length function ‘strlen’. The following code snippet demonstrates the use of ‘utf8_decode’ with ‘strlen’.

Code snippet:

$utfCharacters = 'êð';

$characterCount = strlen ( utf8_decode ( $utfCharacters );

echo $characterCount;

Output:

2

PHP String Length Function alternative ‘preg_match_all’

int preg_match_all (string pattern, string string)

This alternative to PHP String Length function matches all occurrence of pattern in string and returns the total number of matches. Following code snippets demonstrates the use of ‘preg_match_all’ function.

Code snippet:

$utfCharacters = 'êð';

$length = preg_match_all( '(.)su', $utfCharacters );

echo $length;

Output:

2

For more detailed, PHP Tutorials, check out Udemy.com courses