What are some best practices for determining if a year is a leap year in PHP?
To determine if a year is a leap year in PHP, you can use the following logic: a year is a leap year if it is divisible by 4, unless it is divisible by 100 but not by 400. This means that a year like 2000 is a leap year because it is divisible by 400, while a year like 1900 is not a leap year because it is divisible by 100 but not by 400.
function isLeapYear($year) {
return (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0);
}
// Usage
$year = 2000;
if(isLeapYear($year)) {
echo $year . " is a leap year.";
} else {
echo $year . " is not a leap year.";
}
Keywords
Related Questions
- What common syntax errors should be avoided when working with PHP code for styling?
- What are some best practices for structuring SQL queries in PHP to retrieve data from multiple tables efficiently?
- In what situations would using .env files or environment variables be a more secure option for storing sensitive data in PHP applications?