This is a basic PHP program which describes how to write Leap Year program in PHP. Below examples will help you in the better understanding of the if…else concept in PHP programming language.
We are using the if…else statement to check whether the entered year is Leap year or not. Copy the below code and execute it with the help of PHP compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <html> <head> <title>Leap Year Program in PHP</title> </head> <body> <form method="post"> Enter a Number: <input type="text" name="year"> <input type="submit" value="Submit"> </form> </body> </html> <?php /** * Leap Year Program in PHP */ if($_POST) { //get the year $year = $_POST['year']; //check if entered value is a number if(!is_numeric($year)) { echo "Strings not allowed, Input should be a number"; return; } //multiple conditions to check the leap year if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) ) { echo "$year is a Leap Year"; } else { echo "$year is not a Leap Year"; } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.