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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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 addslashes function – Quote string with slashes
  2. PHP function abs – Absolute value
  3. PHP function strcmp() – Binary safe string comparison
  4. PHP Function strncasecmp() – Binary safe case-insensitive string comparison of the first n characters
  5. PHP bindec function – Convert a number between arbitrary bases
  6. PHP atan function – Arc tangent
  7. PHP Function str_split() – Convert a string to an array
  8. “Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP
  9. PHP function chunk_split() – Split a string into smaller chunks
  10. PHP atan2 function – Arc tangent of two variables

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright