[英]My else statement is not echoing the variable
它是用PHP編寫的。 我的if工作正常,但我的其他沒有回音。 $ start和$ end是工作變量。
<select name="trip" class="select-trip">
<?php foreach ( $wp_trips as $trip ) {
if ($trip->available && $is_day_trip){
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php echo $start;
} else {
echo $start . ' T/M ' . $end;
}
} ?>
</select>
您應在if語句之前聲明$start
和$end
變量,並將option
標簽放在if statement
之前if statement
並在if statement
之后將其關閉
<select name="trip" class="select-trip">
<?php
foreach ( $wp_trips as $trip ) {
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));
?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php
if ($trip->available && $is_day_trip){
echo $start;
} else {
echo $start . ' T/M ' . $end;
}
?>
</option>
<?php
}
?>
</select>
此代碼中的問題是$ start和$ end僅在該if語句中具有作用域。 為了在if語句之外獲取$ start和$ end的值,顯然應該在if語句之外進行變量聲明。
例如,您可以像下面這樣:
foreach ($wp_trips as $trip) {
$checker = $trip->available && $is_day_trip;
$start = '';
$end = '';
if ($checker) {
$start = date_i18n("d/m/Y", strtotime($trip->start_date));
$end = date_i18n("d/m/Y", strtotime($trip->end_date));
}
if ($checker && !empty($start)) {?>
<option value="<?php echo $trip->remote_ID; ?>">
<?php echo $start;?>
</option>
} else {
echo $start . ' T/M ' . $end;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.