Wednesday 24 March 2021

Php: strlen: Get string length

Signature

strlen ( string $string ) : int

 

Description

Return the length of given string. If the stinng is empty, it return 0.

 

Example

strlen('Hello') return 5 bytes

strlen('Helloته') return 9 bytes

 

Note:

strlen() method return number of bytes consumed by this string, rather than number of characters.

 

strlen_demo.php

#!/usr/bin/php

<?php

   $str1 = 'Hello';

   // strlen function do not return number of chars, it return number of bytes
   $str2 = 'Helloته'; 

   $length1 = strlen($str1);
   $length2 = strlen($str2);

   echo "\$str1 : $str1, length : $length1 \n";
   echo "\$str2 : $str2, length : $length2 \n";
?>

 

Output

$./strlen_demo.php 

$str1 : Hello, length : 5 
$str2 : Helloته, length : 9  

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment