51

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";
    }
?>
1
  • 1
    @Jack Maney - I believe that's an auto WordPress function that outputs the given field name. Commented Mar 6, 2012 at 22:25

9 Answers 9

99
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

Sign up to request clarification or add additional context in comments.

1 Comment

There should be an ! before your empty call, as that is what the OP is looking for.
22

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

3 Comments

The OP should be using a plain, simple if ($web). No need for empty here, it just has the potential to hide mistakes unnecessarily.
I don't see how if(!empty($var)) can create confusion, but I do agree that if ($var) is simpler.
Because empty has the specific purpose of suppressing errors for nonexistent variables. You don't want to suppress errors unless you need to. The Definitive Guide To PHP's isset And empty explains the problem in detail.
14

Simply use if ($web). This is true if the variable has any truthy value.

You don't need isset or empty since you know the variable exists, since you have just set it in the previous line.

1 Comment

This is the best answer. It seems few people know this.
3

I don't see how if(!empty($var)) can create confusion, but I do agree that if ($var) is simpler. – vanneto Mar 8 '12 at 13:33

Because empty has the specific purpose of suppressing errors for nonexistent variables. You don't want to suppress errors unless you need to. The Definitive Guide To PHP's isset And empty explains the problem in detail. – deceze♦ Mar 9 '12 at 1:24

Focusing on the error suppression part, if the variable is an array where a key being accessed may or may not be defined:

  1. if($web['status']) would produce:

    Notice: Undefined index: status

  2. To access that key without triggering errors:
    1. if(isset($web['status']) && $web['status']) (2nd condition is not tested if the 1st one is FALSE) OR
    2. if(!empty($web['status'])).

However, as deceze♦ pointed out, a truthy value of a defined variable makes !empty redundant, but you still need to remember that PHP assumes the following examples as FALSE:

  • null
  • '' or ""
  • 0.0
  • 0
  • '0' or "0"
  • '0' + 0 + !3

So if zero is a meaningful status that you want to detect, you should actually use string and numeric comparisons:

  1. Error free and zero detection:

    if(isset($web['status'])){
      if($web['status'] === '0' || $web['status'] === 0 ||
         $web['status'] === 0.0 || $web['status']) {
        // not empty: use the value
      } else {
        // consider it as empty, since status may be FALSE, null or an empty string
      }
    }
    

    The generic condition ($web['status']) should be left at the end of the entire statement.

3 Comments

An 3v4l example with a converted object into an array depicting property_exists, isset, array_key_exists and empty shows that isset always detects if the variable has been assigned a non-null value and array_key_exists always detects if the key is accessible.
isset && !empty is redundant. Just !empty will do exactly the same.
@deceze It has been a while but you're right: empty does seem to absorb/suppress all notices/errors, avoiding the "Undefined offset/index" notice to be thrown due to a non-existing key in the array (even in PHP v4.4.9, just tested :). I adjusted the answer, thanks for pointing that out!
1

You're using isset, what isset does is check if the variable is set ('exists') and is not NULL. What you're looking for is empty, which checks if a variable is empty or not, even if it's set. To check what is empty and what is not take a look at:

http://php.net/manual/en/function.empty.php

Also check http://php.net/manual/en/function.isset.php for what isset does exactly, so you understand why it doesn't do what you expect it to do.

1 Comment

This is the best way if you have to check a variable that contains numbers. As in the refernce shown, $e = 0; echo empty($e) will return false too. It does not matter in what format we provide the number. (String, Float, int) See: php.net/manual/en/…
0
if(!empty($web))
{
   echo 'Something';
}

1 Comment

This worked good: <?php $web = get_field('website'); if (!empty($web)) { echo "<span class='field-label'>Website: </span><a href='http://<?php the_field('website'); ?> target='_blank'><?php the_field('website'); ?></a>"; } else { echo "Niente";} ?> But i can't see the true link, I see this output: WEBSITE: target='_blank'>
0
if($var !== '' && $var !== NULL)
{
   echo $var;
}

4 Comments

This is less elegant than deceze's answer -- which is the most professional technique on the page.
deceze's technique does not work when variable have false value and question is about check if variable is not empty.
Then I guess it comes down to what you consider empty. Deceze is very careful to explain that his check is "truthy". If the OP specifically expects a boolean false value, that is not clear in the question. Your unexplained answer does not speak of allowing boolean false values.
I don't Wordpress. Does the_field() return false AND would the OP want to handle it as a non-empty outcome if it does? I reckon not.
0

Your problem is in your use of the_field(), which is for Advanced Custom Fields, a wordpress plugin.

If you want to use a field in a variable you have to use this: $web = get_field('website');.

Comments

0

i hope this will work too, try using"is_null"

<?php 
$web = the_field('website');
if (!is_null($web)) {
?>

....html code here

<?php
} else { 
    echo "Niente";
}
?>

http://php.net/manual/en/function.is-null.php

hope that suits you..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.