Imtiyaaz Posted April 3, 2012 Share Posted April 3, 2012 Hi Im having a bit of trouble with my leap_year() function. If I enter in any year it prints "Leap Year" even if I enter in a year that is not a leap year. Below is my form and php...Please assist <form Method = "POST" ACTION = "leap.php"> <input type = "text" name = "year" /> <br><br> <input type ="submit" name= "check_year" value="Check" /> </form> <?php error_reporting(0); // turn error messages off //Declare variables $Check = $_POST['check_year']; $year = $_POST['year']; if (isset($_POST['check_year'])) { leap_year(); } function leap_year() { if((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0))){ echo "Leap Year"; } else{ echo "Not a leap year"; } } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted April 3, 2012 Share Posted April 3, 2012 Try calling your function like this: leap_year($year); And declaring it like this: function leap_year($year) Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 3, 2012 Share Posted April 3, 2012 Although the logic in your function is correct, you certainly used an over abundance of parens! however, there's no need to write your own logic for this. You can use PHP's checkdate() or even the date() function to do it for you. function leap_year($year) { if(checkdate(2, 29, $year)) { echo "Leap Year"; } else { echo "Not a leap year"; } } or,more simply function leap_year($year) { echo (checkdate(2, 29, $year)) ? "Leap Year" : "Not a leap year"; } Quote Link to comment Share on other sites More sharing options...
btherl Posted April 3, 2012 Share Posted April 3, 2012 Whether that's acceptable depends on how the assignment question was worded Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.