如何在回声中嵌入if语句
我对此感到头疼。 我需要把if语句放在echo中(这个echo是在一个函数中,它实际上是一个表单提交)
这是一部分我的代码的例子。 在这种情况下,我怎么能把这些if语句放在echo中?
<?php echo '<td><select id="depuis" name="depuis">
<option value='4' <?php if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ echo 'selected'; } else { echo ''; } ?> ></option>
<option value='1' <?php if(isset($_POST['depuis']) && $_POST['depuis'] == '1'){ echo 'selected'; } else { echo ''; } ?> >2 ans et moins</option>
<option value='2' <?php if(isset($_POST['depuis']) && $_POST['depuis'] == '2'){ echo 'selected'; } else { echo ''; } ?> >2 à 5 ans</option>
<option value='3' <?php if(isset($_POST['depuis']) && $_POST['depuis'] == '3'){ echo 'selected'; } else { echo ''; } ?> >5 ans et plus</option>
</select>
</td>'
; ?>
一切都是PHP,所以在检查if
之前,需要使用比第一个<?php
每个echo
。 喜欢这个:
<?php
echo '<td><select id="depuis" name="depuis">
<option value="4"';
if(isset($_POST['depuis']) && $_POST['depuis'] == '4') {
echo ' selected';
}
echo ' >Something here maybe?</option>...etc
这将工作 - 虽然我相信它可以简化:
<?php
$out = '
<td>
<select id="depuis" name="depuis">
<option value="4"
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){
$out .= 'selected';
}
$out .= '
></option>
<option value='1'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '1'){
$out .= 'selected';
}
$out .= '
>2 ans et moins</option>
<option value='2'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '2'){
$out .= 'selected';
}
$out .= '
>2 à 5 ans</option>
<option value='3'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '3'){
$out .= 'selected';
}
$out .= '
>5 ans et plus</option>
</select>
</td>
';
echo $out;
?>
Meilleurs voeux ...
使用内联if语句:
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');
链接地址: http://www.djcxy.com/p/12011.html