如果变量为空,如何隐藏DIV

时间:2018-07-24 01:01:32

标签: html css variables display

我里面有一个div和一个变量,如果变量为空,我想隐藏div。有什么线索吗?例如:

<div class='h-box'>
<h1>text<h1/>
<p><?php echo regular_he(); ?><p/>
</div>

3 个答案:

答案 0 :(得分:1)

您可以使用PHP is_empty函数检查其是否为空:

<?php if(is_empty($var_to_check)): ?>
  <div class='h-box'>
    <h1>text<h1/>
    <p><?php echo regular_he(); ?><p/>
  </div>
<?php endif; ?>

查看文档:{​​{3}}

答案 1 :(得分:1)

仅当变量为空或为null时,您才能检查变量

if($var == "" && $var == NULL){
  // Do your work
}   

对于数组,

if(!empty($array_name)){
  // Do your work
}

完整示例,

<?php
    $name = "Akash";
    $test_array = [];
    function regular_he(){
         return "Something";
    }
?>
 <!-- For Variable -->
<?php if($name != "" && $name != NULL): ?>
    <div class='h-box'>
        <h1>text<h1/>
        <p><?php echo regular_he(); ?><p/>
    </div>
<?php endif; ?>


<!-- For Array -->
<?php if(!empty($test_array)): ?>
    <div class='h-box'>
        <h1>text<h1/>
        <p><?php echo regular_he(); ?><p/>
    </div>
<?php endif; ?>

答案 2 :(得分:0)

在这里https://stackoverflow.com/a/60402179/3832706非常简单。它显示了如何在2行代码中隐藏和显示div。

相关问题