PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP Program to Check Leap Year
Photo Credit to CodeToFun
π Introduction
In the realm of programming, determining whether a given year is a leap year is a common requirement. A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year. Checking for leap years involves implementing specific rules to validate the year.
In this tutorial, we will explore a PHP program designed to check whether a given year is a leap year.
π Example
Let's delve into the PHP code that accomplishes this task.
<?php
// Function to check if a year is a leap year
function isLeapYear($year)
{
// Leap year conditions
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) return true; // True, it's a leap year
else return false; // False, it's not a leap year
}
// Driver program
// Replace this value with the year you want to check
$year = 2024;
// Call the function to check if the year is a leap year
if (isLeapYear($year)) echo "$year is a leap year.\n";
else echo "$year is not a leap year.\n";
?>
π» Testing the Program
To test the program with different years, modify the value of $year in the code.
2024 is a leap year.
Run the script to check if the specified year is a leap year.
π§ How the Program Works
- The program defines a function isLeapYear that takes an integer $year as input and returns true if the year is a leap year, and false otherwise.
- The function checks leap year conditions: the year must be divisible by 4 and not divisible by 100, or it must be divisible by 400.
- Inside the main program, replace the value of $year with the desired year you want to check.
- The program calls the isLeapYear function and prints the result using echo.
π Between the Given Range
Let's dive into the php code that checks for leap years in the specified range.
<?php
// Function to check if a year is a leap year
function isLeapYear($year)
{
return ($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0);
}
// Range of years from 2024 to 2050
$startYear = 2024;
$endYear = 2050;
// Display leap years in the specified range
echo "Leap Years in the range $startYear to $endYear:\n";
for ($year = $startYear;$year <= $endYear;$year++)
{
if (isLeapYear($year))
{
echo $year . " ";
}
}
?>
π» Testing the Program
The provided code is already set to check for leap years in the range from 2024 to 2050.
Leap years in the range 2024 to 2050: 2024 2028 2032 2036 2040 2044 2048
You can run the script as is or modify the range as needed.
π§ How the Program Works
- The program defines a function isLeapYear that checks if a given year is a leap year using the leap year rule.
- It sets the range of years from 2024 to 2050 using variables $startYear and $endYear.
- The program then iterates through each year in the range, using a for loop.
- For each year, it checks if it's a leap year using the isLeapYear function and prints the leap years.
π§ Understanding the Concept of Leap Year
Before delving into the code, let's understand the concept of leap years. Leap years are years that are evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking leap years.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (PHP Program to Check Leap Year), please comment here. I will help you immediately.