如果变量为空,如何隐藏DIV [英] How to Hide a DIV if Variable is Null
本文介绍了如果变量为空,如何隐藏DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我里面有一个div和一个变量,如果变量为空,我想隐藏div。有什么线索吗?例如:
I have a div and a variable inside it, and would like to hide the div if the variable is empty. Any clues? For example:
<div class='h-box'>
<h1>text<h1/>
<p><?php echo regular_he(); ?><p/>
</div>
推荐答案
只有当变量为空或null
You can check variable by only if that is empty or 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; ?>
这篇关于如果变量为空,如何隐藏DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文