Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";     } ?> 
like image 516
Luca Frank Guarini Avatar asked Mar 06 '12 22:03

Luca Frank Guarini


People also ask

How do you check whether a variable is not empty in PHP?

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.

How do you check if a variable has a value in PHP?

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.

Does empty check for NULL PHP?

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.


1 Answers

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

like image 197
Rodney Folz Avatar answered Sep 18 '22 05:09

Rodney Folz