【问题标题】:How do I hide several lines if php variable is empty?如果 php 变量为空,如何隐藏几行?
【发布时间】:2014-09-11 12:26:42
【问题描述】:

作为 PHP 的学习者,如果所选变量为空,我会努力避免显示多行。我可以让它在基本层面上工作,但当要隐藏的内容变得更加复杂时,我会迷失方向。我的目标是这样的:

<?php if (isset($g1)) { ?>
((This part should not display if variable $g1 is empty))
<?php } ?>

我的代码如下所示:

<?php if (isset($g1)) { ?>
<a href="img/g1.png"  class="lightbox"  rel="tooltip" data-original-title="<?php print $g1; ?>" data-plugin-options='{"type":"image"}'>
<img class="img-responsive img-rounded wbdr4" src="img/g1.png">
</a>
<?php } ?>

在上面,当变量 g1 为空时,工具提示不会显示,但其余部分会显示。我是新来的,我希望我的问题格式正确。帮助表示赞赏。

【问题讨论】:

  • 看起来他们正在使用那个.. 但是他们的编码风格在我眼中非常难以理解。
  • 使用 if ($g1 != NULL) { //img 标签 }
  • 我建议先看看我的答案,因为这里的每个答案都误解了你的问题。 (或者我是白痴,其他人都是对的:P)
  • 你可以使用empty()函数。它会处理所有的。 php.net/manual/en/function.empty.php
  • 只是说明我感谢所提供的帮助。这是一个了不起的社区! :-)

标签: php variables show-hide is-empty


【解决方案1】:

这是我的答案,似乎与其他人完全不同,我不知道为什么。

做 OP 想要的,一种方法是扩展 php 标签以包含所有。

<?php 
    if (isset($g1)) { 
        echo "<a href='img/g1.png'  class='lightbox'  rel='tooltip' data-original-title='".$g1."' data-plugin-options='{\"type\":\"image\"}'>";
        echo "<img class='img-responsive img-rounded wbdr4' src='img/g1.png'>";
        echo "</a>";
    } 
?>

【讨论】:

    【解决方案2】:

    你可以使用empty()函数:

    <?php if (isset($g1) && !empty($g1)) { ?>
    ((This part should not display if variable $g1 is empty))
    <?php } ?>
    

    <?php if (isset($g1) && $g1 !== '') { ?>
    ((This part should not display if variable $g1 is empty))
    <?php } ?>
    

    【讨论】:

    • 如果它不为空,那么肯定必须设置它
    • isset 只检查 null,你仍然可以使用 empty() 检查空字符串
    • 不需要。就像isset() 一样,empty() 与未声明的变量一起使用时不会发出警告,如果未声明,则应返回 FALSE
    【解决方案3】:
    Try this code:
    <?php if (!empty($g1)) { ?>
    ((This part should not display if variable $g1 is empty))
    <?php } ?>
    

    【讨论】:

      【解决方案4】:

      isset() 函数检查变量是否已设置,即使设置为空字符串。 empty() 函数检查变量是否未设置或设置为空字符串。

      <?php
      $x = '';
      if (isset($x)) print('$x is set');
      if (empty($x)) print('$x is not set or is empty');
      if (isset($x) && empty($x)) print('$x is set and is empty');
      if (!empty($x)) print('$x is set and not empty'); // won't emit warning if not set
      

      【讨论】:

        【解决方案5】:

        在 php 中隐藏某些东西非常简单,您可以通过多种方式使用它,下面是它的样子

        <?php if(isset($g1) == ""): ?>
           //The $g1 is empty so anything here will be displayed
        <?php else: ?>
           //The $g1 is NOT empty and anything here will be displayed
        <?php endif; ?>
        

        【讨论】:

        • isset($g1) == ""... 真的吗?
        【解决方案6】:
        <?php if (isset($g1) && $g1!='') { ?>
        <a href="img/g1.png"  class="lightbox"  rel="tooltip" data-original-title="<?php print $g1; ?>" data-plugin-options='{"type":"image"}'>
        <img class="img-responsive img-rounded wbdr4" src="img/g1.png">
        </a>
        <?php } ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-05
          • 1970-01-01
          • 2021-01-18
          相关资源
          最近更新 更多