我使用以下結構爲我的foreach我將如何使用相同格式的if語句?使用如果與我的foreach結構
<?php foreach($property as $section):?>
if(!empty($section))
{
echo <p>data here </p>;
}else{
html
}
<?php endforeach;?>
我使用以下結構爲我的foreach我將如何使用相同格式的if語句?使用如果與我的foreach結構
<?php foreach($property as $section):?>
if(!empty($section))
{
echo <p>data here </p>;
}else{
html
}
<?php endforeach;?>
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
endif;
endforeach;?>
OR
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
else:
echo "html";
endif;
endforeach;?>
固定打開/關閉標記和回聲報價 –
@nicogawenda謝謝... :) –
不確定你的意思,但你沒有有效的PHP。
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
我認爲你正在尋找這一點。
<?php foreach($property as $section): ?>
<?php
if(!empty($section)) :
echo <p>data here </p>;
else :
html
endif;
endforeach; ?>
簡寫:
<?php foreach($property as $section):?>
<?php if(!empty($section)):?>
<p>data here </p>;
<?php else: ?>
html
<?php endif;?>
<?php endforeach;?>
OTHER:
<?php foreach($property as $section)
{
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
?>
這是包含}elseif{
也是一個例子:
<?php foreach($property as $section):?>
<?php if(!empty($section)): ?>
<p>data here </p>
<?php elseif ([another condition]): ?>
...
<?php else: ?>
html
<?php endif: ?>
<?php endforeach;?>
整個文檔是在這裏:http://php.net/manual/en/control-structures.alternative-syntax.php
我覺得這個最好的解決辦法是:
<?php
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}
else
{
?>
<div id="dosome">really crazy html stuff here</div>
<?php
}
}
?>
「相同的格式中的」 - 這是什麼意思? –
@SergioTulentsev使用endforeach和: –