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"; } ?>
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
empty() This function checks whether a variable evaluates to what PHP sees as a "falsy"(a.k.a empty) value. This being said, a variable is empty if it's undefined, null, false, 0 or an empty string.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With