PHP - If variable is not empty, echo some html cod

2020-02-07 17:48发布

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";
    }
?>

10条回答
对你真心纯属浪费
2楼-- · 2020-02-07 18:26

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楼-- · 2020-02-07 18:27

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..

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-07 18:30

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.

查看更多
干净又极端
5楼-- · 2020-02-07 18:31

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.

查看更多
We Are One
6楼-- · 2020-02-07 18:34

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.

查看更多
在下西门庆
7楼-- · 2020-02-07 18:35

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');.

查看更多
登录 后发表回答