• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP function strlen() – Get string length

by admin

The strlen() function is used to return the length of a specific string. This function was introduced in PHP3. The strlen() function takes just one parameter (the string) and returns the number of characters in it.

Syntax:

strlen(string $string): int

Behind the scenes, strlen() actually counts the number of bytes in your string, as opposed to the number of characters. It is for this reason that multibyte strings should be measured with mb_strlen().

Parameters

Parameter Description
string The string being measured for length.

Return Values

Returns the number of characters in a string, and 0 if the string is empty.

Examples

Example 1:

print strlen("Foo") . "\n"; // 3
print strlen("Goodbye, Perl!") . "\n"; // 14

Example 2:

<?php
$text = 'aeiou';
echo strlen($text)."<br>";
$str = ' ab cd ';
echo strlen($str);
?>

The output of the above program is:

5
7

Example 3:

<?php
$str = 'abcdef';
echo strlen($str); // 6

$str = ' ab cd ';
echo strlen($str); // 7
?>

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP base_convert function – Convert a number between arbitrary bases
  2. PHP function chunk_split() – Split a string into smaller chunks
  3. How to test a PHP script
  4. PHP function chop() – Alias of rtrim()
  5. PHP function stripslashes() – Un-quotes a quoted string
  6. PHP function strcmp() – Binary safe string comparison
  7. PHP atan function – Arc tangent
  8. PHP function acos – Arc cosine
  9. PHP function convert_cyr_string() – Convert from one Cyrillic character set to another
  10. PHP ceil function – Round fractions up

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright