PHP program to find the number of characters in the string


To find the number of characters in the string, the PHP code is as follows −

Example

 Live Demo

<?php
   $my_string = "Hi there, this is a sample ";
   $len = mb_strlen($my_string);
   print_r("The number of characters in the string is ");
   echo $len;
?>

Output

The number of characters in the string is 27

Above, a string is defined that has more than the required spaces in between −

$my_string = "Hi there, this is a sample ";

Next, the length of the string is computed using the ‘strlen’ function. This length is assigned to a variable. This will give the characters present in the string including the spaces as well −

$len = mb_strlen($my_string);

The length computed is then printed on the screen −

print_r("The number of characters in the string is ");
echo $len;

Updated on: 17-Aug-2020

648 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements