PHP—如果变量不是空的,则回显一些html代码

时间:2023-01-05 21:37:44

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

10 个解决方案

#1


68  

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

#2


13  

isset will return true even if the variable is "". isset returns false only if a variable is null. What you should be doing:

即使变量是“”,isset也将返回true。isset仅在变量为空时返回false。你应该做的是:

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

This will check that he variable is not empty.

这将检查变量是否为空。

Hope this helps

希望这有助于

#3


5  

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

简单地使用如果(web)。如果变量具有任何真实值,则为真。

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

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

#4


1  

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

#5


1  

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

#6


1  

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

希望适合你. .

#7


0  

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:

您正在使用isset, isset所做的是检查变量是否被设置('exist ')并且不是NULL。你要找的是空的,它检查变量是否为空,即使它已经设置好了。

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.php for what isset does exactly, so you understand why it doesn't do what you expect it to do.

还要检查http://php.net/manual/en/function.isset.php,看看isset到底做了什么,这样您就能理解为什么它不做您期望它做的事情。

#8


0  

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

#9


0  

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

我不认为如果(!empty($var))会造成混乱,但我同意if($var)更简单。- vanneto 3月8日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

因为empty是用来抑制不存在变量的错误的。除非需要,否则您不会想要抑制错误。PHP的isset和empty的最终指南详细解释了这个问题。——deceze♦3月9日的12一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(empty($web['status'])) or if($web['status']) would produce:

    Error: Undefined index: status

    错误:未定义的指数:状态

  2. To access that key without triggering errors:

    在不触发错误的情况下访问该密钥:

    1. if(isset($web['status']) && $web['status']) or
    2. 如果收取(web(的地位)美元)& & $ web(“状态”))
    3. if(isset($web['status']) && !empty($web['status'])).
    4. 如果收取(web(的地位)美元)& & !空(web(的地位))美元)。

    Why? The second condition is not tested if the first one (isset) is FALSE.

    为什么?如果第一个条件(isset)为假,则不测试第二个条件。

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:

然而,随着deceze♦指出,真相定义变量的值使得!空冗余,但你仍然需要记住,PHP错误假定下面的例子:

  • null
  • '' or ""
  • ”或“”
  • 0.0
  • 0.0
  • 0
  • 0
  • '0' or "0"
  • “0”或“0”
  • '0' + 0 + !3
  • 0 + 0 + 3

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

所以如果0是一个有意义的状态你想要检测,你应该使用字符串和数值比较:

  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.

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

#10


-4  

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代码”将显示出来。如果它是空的,什么也不会发生。

#1


68  

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

#2


13  

isset will return true even if the variable is "". isset returns false only if a variable is null. What you should be doing:

即使变量是“”,isset也将返回true。isset仅在变量为空时返回false。你应该做的是:

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

This will check that he variable is not empty.

这将检查变量是否为空。

Hope this helps

希望这有助于

#3


5  

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

简单地使用如果(web)。如果变量具有任何真实值,则为真。

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

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

#4


1  

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

#5


1  

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

#6


1  

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

希望适合你. .

#7


0  

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:

您正在使用isset, isset所做的是检查变量是否被设置('exist ')并且不是NULL。你要找的是空的,它检查变量是否为空,即使它已经设置好了。

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.php for what isset does exactly, so you understand why it doesn't do what you expect it to do.

还要检查http://php.net/manual/en/function.isset.php,看看isset到底做了什么,这样您就能理解为什么它不做您期望它做的事情。

#8


0  

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

#9


0  

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

我不认为如果(!empty($var))会造成混乱,但我同意if($var)更简单。- vanneto 3月8日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

因为empty是用来抑制不存在变量的错误的。除非需要,否则您不会想要抑制错误。PHP的isset和empty的最终指南详细解释了这个问题。——deceze♦3月9日的12一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(empty($web['status'])) or if($web['status']) would produce:

    Error: Undefined index: status

    错误:未定义的指数:状态

  2. To access that key without triggering errors:

    在不触发错误的情况下访问该密钥:

    1. if(isset($web['status']) && $web['status']) or
    2. 如果收取(web(的地位)美元)& & $ web(“状态”))
    3. if(isset($web['status']) && !empty($web['status'])).
    4. 如果收取(web(的地位)美元)& & !空(web(的地位))美元)。

    Why? The second condition is not tested if the first one (isset) is FALSE.

    为什么?如果第一个条件(isset)为假,则不测试第二个条件。

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:

然而,随着deceze♦指出,真相定义变量的值使得!空冗余,但你仍然需要记住,PHP错误假定下面的例子:

  • null
  • '' or ""
  • ”或“”
  • 0.0
  • 0.0
  • 0
  • 0
  • '0' or "0"
  • “0”或“0”
  • '0' + 0 + !3
  • 0 + 0 + 3

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

所以如果0是一个有意义的状态你想要检测,你应该使用字符串和数值比较:

  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.

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

#10


-4  

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代码”将显示出来。如果它是空的,什么也不会发生。