PHP strlen() Function

PHP strlen() function “returns the length of a given string.” 

Please note that strlen returns the number of bytes rather than the number of characters in a string.

Syntax

strlen(string)

Parameters

 string(required): It specifies the string to check.

Return value

It returns the length of the string, including all the whitespaces and special characters. If the string is empty, this function returns 0.

Visual RepresentationVisual Representation of PHP strlen() Function

Example 1: How to Use strlen() function

<?php

$text = 'AppDividend';

$text2 = 'App Dividend';

echo strlen($text)."\n";

echo strlen($text2);

Output

11
12 

Example 2: Passing an empty stringVisual Representation of Passing an empty string

<?php

$text = '';
echo strlen($text);

Output

0

Example 3: Passing a Null Value

<?php

$text = null;

echo strlen($text);

Output

0

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.