2014-12-02 62 views
1

我想呼应的一些HTML,如果一个变量不是empy,为了这个,我知道我能做到以下几点:显示HTML如果多个变量的一个非空

if (!empty($test)) { 
?> 
    <p>Some nice html can go here</p> 
<?php 
} 
else 
{ 
echo "It's empty..."; 
} 

我如何能做到这一点的几个变量?所以如果其中一个变量不是空的,那么回声的HTML?这是否会这样做?

if (!empty($test || $image || $anothervar)) { 
?> 
    <p>Some nice html can go here</p> 
<?php 
} 
else 
{ 
echo "It's empty..."; 
} 

回答

1

与刚刚尝试:

if (!empty($test) || !empty($image) || !empty($anothervar)) { 
    // ... 
} 
1

您应该检查每个变量:

!empty($test) || !empty($image) || !empty($anothervar) 
1

empty功能并不需要多个参数。

因此,您需要分别为每个变量使用用户empty

最后的代码应该是:

if (!empty($test) || !empty($image) || !empty($anothervar)) { 
+0

事实上,OP的代码将_唯一的one_参数传递给'empty'。 – mudasobwa 2014-12-02 10:15:13

1

只是检查所有的三个变量。

另外,我建议你嵌入你的PHP在HTML中有更好的可读的文件,像这样:

<?php if (!empty($test) || !empty($image) || !empty($anothervar)): ?> 
    <p>Some nice html can go here</p> 
<?php else: ?> 
    It's empty... 
<?php endif; ?> 
1

只是另一种解决方案:

if(empty($test) and empty($image) and empty($anothervar)) { 
    echo "It's completely empty..."; 
} else { 
?> 
    <p>Some nice html can go here</p> 
<?php 
} 

或者,如果你有很多要检查的变量:

$check = array("test","image","anothervar"); 
$empty = true; 
foreach($check as $var) { 
    if(! empty($$var)) { 
     $empty = false; 
    } 
} 
if($empty) { 
    echo "It's completely empty..."; 
} else { 
?> 
    <p>Some nice html can go here</p> 
<?php 
} 
相关问题