How to check whether a given year is a leap year using php.

<?php
function isLeapYear($year) {
    return (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0));
}

$testYear = 2024;

if (isLeapYear($testYear)) {
    echo "$testYear is a leap year.";
} else {
    echo "$testYear is not a leap year.";
}
?>
  • The script defines a function isLeapYear that checks whether a given year is a leap year.
  • The function uses the leap year rule: a year is a leap year if it is divisible by 4 but not divisible by 100 unless it is divisible by 400.
  • The script then calls the function with a sample year ($testYear) and prints whether it is a leap year or not.

Post your Answer