How can PHP developers accurately determine if a given year is a leap year within their code?
To accurately determine if a given year is a leap year in PHP, developers can use the following logic: a leap year is divisible by 4, unless it is divisible by 100 but not by 400. This can be implemented in PHP by creating a function that checks these conditions and returns true if the year is a leap year, and false if it is not.
function isLeapYear($year) {
if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) {
return true;
} else {
return false;
}
}
// Example usage
$year = 2020;
if (isLeapYear($year)) {
echo "$year is a leap year.";
} else {
echo "$year is not a leap year.";
}