我正在上课检查假期。我制作了特定假期的函数,并使用一个总体函数来查看日期是否是假期,而不是一个假期。
我收到以下错误:
Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\mgmt\classes\Holidays.php on line 52
这是我的代码:
<?php
class Holidays {
//private member variables
private $date;
//constructors
public function Holidays() {
$this->$date = date("Y-m-d");
}
//setters
public function setDate($date) {
$this->$date = $date;
}
//getters
public function getDate() {
return $this->$date;
}
//member public functions
public function isNewYears($date) {
return ($date == date('Y', $date)."-01-01" ? true : false);
}
public function isMLKDay($date) {
return ($date == date("Y-m-d", strtotime("third Monday of January ".date('Y', $date)) ? true : false));
}
public function isValentinesDay($date) {
return ($date == date('Y', $date)."-02-14" ? true : false);
}
public function isPresidentsDay($date) {
return ($date == date("Y-m-d", strtotime("third Monday of February ".date('Y', $date)) ? true : false));
}
public function isEaster($date) {
return ($date == date("Y-m-d", easter_date($date)) ? true : false);
}
public function isMemorialDay($date) {
return ($date == date("Y-m-d", strtotime("last Monday of May ".date('Y', $date)) ? true : false));
}
public function isLaborDay($date) {
return ($date == date("Y-m-d", strtotime("first Monday of September ".date('Y', $date)) ? true : false));
}
public function isThanksgiving($date) {
return ($date == date("Y-m-d", strtotime("fourth Thursday in November".date('Y', $date)) ? true : false));
}
public function isChristmas($date) {
return ($date == date('Y', $date)."-12-25" ? true : false);
}
public function isHoliday($date) {
if (isNewYears($date))
else if (isMLKDay($date))
else if (isValentinesDay($date))
else if (isPresidentsDay($date))
else if (isEaster($date))
else if (isMemorialDay($date))
else if (isLaborDay($date))
else if (isThanksgiving($date))
else if (isChristmas($date))
else return false;
}
}
?>
它在中的第一个else语句上引发错误isHoliday()
。如果那不是正确的结构,那我应该怎么做呢?
您正在尝试做一个逻辑或,可以用 ||
public function isHoliday($date) {
return $this->isNewYears($date)
|| $this->isMLKDay($date)
|| $this->isValentinesDay($date)
|| $this->isPresidentsDay($date)
|| $this->isEaster($date)
|| $this->isMemorialDay($date)
|| $this->isLaborDay($date)
|| $this->isThanksgiving($date)
|| $this->isChristmas($date);
}
您还可以删除if / else,而只需返回布尔表达式本身的结果。
编辑-您发布的代码还有其他错误-
构造函数应使用$ this-> date,而不是$ this-> $ date
public function Holidays() {
$this->date = date("Y-m-d");
}
除此之外,如果您需要$ date的当前实例化值,则应使用表示法
$this->date
该函数Easter_date不存在
public function isEaster($date) {
return ($date == date("Y-m-d", easter_date($date)) ? true : false);
}
解决此问题后,您应该可以将其用作-
$holidays = new Holidays();
$isHoliday = $holidays->isHoliday(date("Y-m-d"));
if($isHoliday) {
echo "Today is a holiday!";
} else {
echo "Today is not a holiday! :(";
}
由于您的函数似乎都是辅助函数,因此建议您改用php中的静态函数:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句