PHP - If variable is not empty, echo some html code
I would like to display some html code if a variable is not empty, else I would like to display nothing.
I've tried this code but doesn't work:
<?php
$web = the_field('website');
if (isset($web)) {
?>
<span class="field-label">Website: </span><a href="http://<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a>
<?php
} else {
echo "Niente";
}
?>
Solution 1:
if (!empty($web)) {
?>
<span class="field-label">Website: </span><a href="http://<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a>
<?php
} else { echo "Niente";}
http://us.php.net/manual/en/function.empty.php
Solution 2:
isset
will return true even if the variable is "". isset
returns false only if a variable is null. What you should be doing:
if (!empty($web)) {
// foo
}
This will check that he variable is not empty.
Hope this helps