String Length in PHP



Most of the time it needs to find out the total size of a declared string. This helps during coding in form validation, in form input or something like that. Php has a built-in function to find out the total size of a string. The function is strlen() ; This function usually takes one parameter that is string and returns the size of that string. We can understand the process by below code


<?php

    $bird = "Crow is a black bird";
    $size = strlen($bird);
    echo "The size is " . $size;
?>

Output


The size is 20

  The output of this program is "The size is 20" that means string "Crow is a Blackbird" has 20 characters. It needs to remember in the programming world, space is also regarded as a character. We can see other examples also from below


<?php
    
    $program = "PHP is a popular web programming language in today's world";
    echo strlen($program);

?>

The output of the PHP code for example 2 is


58

In this way, we can easily find out the size of a string.