WoShiNiBaBa WoShiNiBaBa - 5 days ago 5
HTML Question

Add specific text when there's no text in it

This is my code:

<?php if($tagline != "")
echo $tagline;
else
echo "Welcome to my Site!";
?>


For some reason, when the tagline is empty, it doesn't add the text.

Answer

I'd try another approach for the check of the $tagline variable

if (strlen(trim($tagline)) > 0) {
    echo $tagline;
} else {
    echo "Welcome to my Site!";
}

or something that you would use in a couple of years :) :

echo (strlen(trim($tagline)) > 0) ? $tagline : "Welcome to my Site!";
Comments