[英]Display html if one of more variables are not empty
如果變量不是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...";
}
您應該檢查每個變量:
!empty($test) || !empty($image) || !empty($anothervar)
只需嘗試:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
// ...
}
空函數不帶多個參數。
因此,您需要為每個變量單獨empty
用戶。
最終代碼應為:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
只需檢查所有三個變量。
另外,我建議您將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; ?>
只是另一個解決方案:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.