PHP - 如果变量不为空,则回显一些 html 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9592855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:12:37  来源:igfitidea点击:

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

phphtmlvariables

提问by Luca Frank Guarini

I would like to display some html code if a variable is not empty, else I would like to display nothing.

如果变量不为空,我想显示一些 html 代码,否则我不想显示任何内容。

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

回答by Rodney Folz

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

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

回答by vanneto

issetwill return trueeven if the variable is "". issetreturns false only if a variable is null. What you should be doing:

isset即使变量是“”也会返回isset仅当变量为null时才返回 false 。你应该做什么:

if (!empty($web)) {
    // foo
}

This will check that he variable is not empty.

这将检查变量是否为空。

Hope this helps

希望这可以帮助

回答by deceze

Simply use if ($web). This is trueif the variable has any truthyvalue.

只需使用if ($web). 这是true变量是否具有任何值。

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

您不需要isset或者empty因为您知道变量存在,因为您刚刚在上一行中设置了它。

回答by CPHPython

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 emptyhas 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 issetAnd emptyexplains the problem in detail. – deceze? Mar 9 '12 at 1:24

我不明白怎么if(!empty($var))会造成混乱,但我同意这if ($var)更简单。– vanneto 2012 年3 月 8 日,13:33

因为empty具有抑制不存在变量的错误的特定目的。除非需要,否则您不想抑制错误。The Definitive Guide To PHP'sissetempty详细解释了问题。–晕倒?2012 年 3 月 9 日 1:24

Focusing on the error suppression part, if the variable is an arraywhere a keybeing 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'])).
  1. if($web['status'])会产生:

    注意:未定义索引:状态

  2. 要在不触发错误的情况下访问该密钥:
    1. if(isset($web['status']) && $web['status'])(如果第一个条件是,则不测试第二个条件FALSE)或
    2. if(!empty($web['status'])).

However, as deceze?pointed out, a truthyvalue of a defined variable makes !emptyredundant, but you still need to remember that PHP assumes the following examples as FALSE:

然而,作为deceze?指出,定义变量的值会变得!empty多余,但您仍然需要记住 PHP 假定以下示例为FALSE

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

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

因此,如果是您想要检测的有意义的状态,您实际上应该使用字符串和数字比较:

  1. Error free and zerodetection:

    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.

  1. 无差错和检测:

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

    通用条件 ( $web['status']) 应留在整个语句的末尾。

回答by dev verma

if($var !== '' && $var !== NULL)
{
   echo $var;
}

回答by photocurio

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

您的问题在于您使用的是the_field(),这是用于高级自定义字段,一个 wordpress 插件。

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

如果你想在变量中使用一个字段,你必须使用这个:$web = get_field('website');

回答by H. Tom

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

我希望这也能奏效,请尝试使用“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

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

hope that suits you..

希望适合你..

回答by hohner

if(!empty($web))
{
   echo 'Something';
}

回答by pbond

You're using isset, what issetdoes 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:

您正在使用isset,什么isset是检查变量是否已设置(“存在”)而不是NULL。您正在寻找的是empty,它检查变量是否为空,即使它已设置。要检查什么是空的,什么不是,请查看:

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

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

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

还要检查http://php.net/manual/en/function.isset.php到底isset做了什么,所以你明白为什么它没有做你期望它做的事情。

回答by M Steigman

Seems like folks are complicating this a bit. Back to the original question, " ... 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."

似乎人们把这件事复杂化了一点。回到最初的问题,“ ...如果变量不为空,则回显一些 html 代码。” “如果变量不为空,我想显示一些 html 代码,否则我不想显示任何内容。

Easy way:

简单的方法:

<?php if (!empty($var)) echo "Some Html Code Here"; ?>

If your variable is not empty "Some HTML code here" will be displayed. If it is empty nothing will happen.

如果您的变量不为空,将显示“此处的一些 HTML 代码”。如果它是空的,什么都不会发生。