Reputation: 805
I am making a class to check for holidays. I made functions for specific holidays and an overarching function to see if a date is a holiday, not one in particular.
I am getting the following error:
Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\mgmt\classes\Holidays.php on line 52
Here is my code:
<?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;
}
}
?>
It's throwing the error on the first else statement in isHoliday()
. If that's not the proper structure then how should I do it?
Upvotes: 0
Views: 164
Reputation: 8306
You are trying to do a logical or, which can be represented using ||
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);
}
You can also remove the if/else, and simply return the result of the boolean expression itself.
Edit - There are additional errors with your posted code -
Constructor should use $this->date, not $this->$date
public function Holidays() {
$this->date = date("Y-m-d");
}
Further to the above, if you ever need the current instantiated value of $date, then you should use the notation
$this->date
The function easter_date does not exist
public function isEaster($date) {
return ($date == date("Y-m-d", easter_date($date)) ? true : false);
}
After you have fixed this issues, you should be able to use it as -
$holidays = new Holidays();
$isHoliday = $holidays->isHoliday(date("Y-m-d"));
if($isHoliday) {
echo "Today is a holiday!";
} else {
echo "Today is not a holiday! :(";
}
As your functions all seem to be helpers, it might be suggested to look at static functions in php instead :)
Upvotes: 3
Reputation: 684
This fixed the error. If you don't need any code, leave it like this, otherwise in specific sections you can write your code:
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;
}
Upvotes: 1
Reputation: 4742
EDIT: Disregard my answer. Alan Foster's is better and should be the accepted answer IMO. Try:
public function isHoliday($date) {
$this->date = $date;
if (
$this->date->isNewYears($date)
||
$this->date->isMLKDay($date)
||
$this->date->isValentinesDay($date)
||
$this->date->isPresidentsDay($date)
||
$this->date->isEaster($date)
||
$this->date->isMemorialDay($date)
||
$this->date->isLaborDay($date)
||
$this->date->isThanksgiving($date)
||
$this->date->isChristmas($date)
)
{
return true;
}else{
return false;
}
}
Upvotes: 1
Reputation: 324650
You kind of need a statement to execute when the if
passes...
if( isNewYears($date)) do_something_here();
else...
Upvotes: 1