strlen in php

You can check the length of a string by using the strlen() function. If you pass it a string, this function will return its length. For example, the result of this code is 5:

echo strlen(“hello”);

You can use the strlen() function for validating input data. Consider the email address on the sample form, stored in $email. One basic way of validating an email address stored in $email is to check its length. By our reasoning, the minimum length of an email address is six characters—for example, if you have a country code with no second-level domains, a one-letter server name, and a one-letter email address. Therefore, an error could be produced if the address is not at least this length

if (strlen($email) < 6){ 
 echo 'That email address is not valid'; 
 exit; // force termination of execution 

Other example of strlen in php

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

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

$attributes = array('one', 'two', 'three');

if (strlen($attributes) == 0 && !is_bool($attributes)) {
    echo "We are in the 'if'\n";  //  PHP 5.3
} else {
    echo "We are in the 'else'\n";  //  PHP 5.2

echo strlen("Hello");



Leave a Comment