php check if variable length equals a value

It's fine. For example, let's run the following:

<?php

$message = "Hello there!";

if (strlen($message) <= 7){
    echo "It is less than or equal to 7 characters.";
} 
else 
{
    echo "It is greater than 7 characters.";
}
?>

It will print: "It is greater than 7 characters."


You might also want to use the PHP shorthand if/else using the ternary operators (?:).

For example, instead of:

<?php

if (strlen($message) <= 7) {
    echo $actiona;
} else {
    echo $actionb;
}

?>

You can write it as:

<?php echo strlen($message) <= 7 ? $actiona : $actionb; ?>

See How do I use shorthand if / else? for information on the ternary operator.

Tags:

Php