2014-12-02 63 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 
} 
相關問題