【发布时间】:2019-02-17 11:42:15
【问题描述】:
我是 PHP 新手。我有一个选择框,其年份范围从当年开始 - 5(2014 年)到 2050 年。我的选择框包含正确的年份,但我需要将其默认为当年。现在,它默认为 2014 年。我已经为此工作了几个小时。我认为循环中需要 $current_year 变量以某种方式 - 为了设置默认值 - 但这不起作用。在研究并看到各种示例之后,我现在认为我需要在循环中构建一个 if 语句,但我真的不知道。
<form id="calendarForm" name="calendarForm" method="post" action="">
<label for="select_year">Select the year: </label>
<select id="select_year" name="select_year">
<?php
$date = new DateTime();
// Sets the current year
$current_year = $date->format('Y');
// Year to start available options.
$earliest_year = ($current_year - 5);
$year = $earliest_year;
for ($year = $earliest_year; $year <= LATEST_YEAR; $year++) {
echo "<option value='$year'>$year</option>";
}
?>
</select>
【问题讨论】:
-
为什么在 ($current_year-5); 周围有括号?
-
另外,我想知道 $year=$earliest_year 是否是多余的。
-
@RussJ 我想我需要括号。
-
我认为那里没有必要。
标签: php