PHP program to check if a year is leap year or not



To check if a year is leap year or not in PHP, the code is as follows −

Example

 Live Demo

<?php    function year_check($my_year){       if ($my_year % 400 == 0)          print("It is a leap year");       else if ($my_year % 100 == 0)          print("It is not a leap year");       else if ($my_year % 4 == 0)          print("It is a leap year");       else          print("It is not a leap year");    }    $my_year = 1900;    year_check($my_year); ?>

Output

It is not a leap year

A function named ‘year_check’ is defined that takes a year as a parameter. It checks to see if the year can be divided by 400 or 4 completely, if yes, it means it is a leap year. Otherwise, it is a leap year. The value for year is defined outside the function, and the function is called by passing this year as a parameter to it. Relevant output is displayed on the console.

Updated on: 2021-12-20T12:29:09+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements