PHP program to check leap year using the if-else and by using the switch case statement
ADVERTISEMENTS
PHP program to check leap year using the if-else and by using the switch case statement. There are you will learn how to check a year is the leap year or not by using the if-else, and by using the switch case statement.
Samples of the leap years:
2000, 2004, ....., 2016, 2020 are the leap years
Let us consider the examples to understand this through the PHP programs:
1. PHP program to check leap year using the if-else statement:
<?php
// PHP program to check leap year
// using the if-else statement
$y = 1997;
if ($y % 400 == 0) {
echo $y . " is the leap year.\n";
} else if ($y % 100 == 0) {
echo $y . " is not the leap year.\n";
} else if ($y % 4 == 0) {
echo $y . " is the leap year.\n";
} else {
echo $y . " is not the leap year.\n";
}
?>
Output:
1997 is not the leap year.
2. PHP program to check leap year using the switch case statement:
<?php
// PHP program to check leap year
// using the switch case statement
$y = 2004;
$r = $y % 400 == 0 || $y % 100 == 0 || $y % 4 == 0;
switch ($r) {
case 1:
echo $y . " is the leap year.\n";
break;
case 0:
echo $y . " is not the leap year.\n";
break;
default:
echo $y . " is not the leap year.\n";
}
?>
Output:
2004 is the leap year.
Tags:
# write a PHP program to find leap year using logical operator
# write a program to check leap year using the function in PHP
# write a PHP program to find leap year using conditional operator