Bhekstar007 Posted September 26, 2022 Share Posted September 26, 2022 Basically I created a leapyear calculator, but to improve user experience i want it to display a message on the webpage confirming whether or not the year entered is a leapyear without changing the webpage. However I keep getting '1 is not leap year' regardless of the the year I enter. What am I doing wrong? <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Leap year form</title> </head> <body> <?php $year = isset($_GET["leapyear"]); function is_leapyear($year){ if(is_numeric($year)){ if($year%4 ==0) if($year%100 ==0) if($year%400 ==0){ return true; } return false; } } if (isset($_GET["confirm"])){ if (is_leapyear($year)){ echo'<span style="color:#008631;">'; echo"$year is a leap year</span>"; } else { echo'<span style="color:#FF0000;">'; echo "$year is not a leap year</span>"; } } ?> <h1>Leap Year</h1> <form action = "leapyear_selfcall.php" method = "get" > <label for="leapyear">Enter a year</label> <input type="text" name="leapyear"/> <p><input type="submit" name="confirm" value="Check For Leap Year"/></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/ Share on other sites More sharing options...
Barand Posted September 26, 2022 Share Posted September 26, 2022 isset($_GET['leapyear'] returns 1 if it is set (true) so your year is "1". Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601053 Share on other sites More sharing options...
Bhekstar007 Posted September 26, 2022 Author Share Posted September 26, 2022 Ok i have entered this code: $year = isset($_GET["leapyear"]) ? $_GET["leapyear"] : 0; To which it now returns the values I entered but now it showing all entered values as not being leap years. I used 2020 and 2021 as the test values. Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601054 Share on other sites More sharing options...
Solution Barand Posted September 26, 2022 Solution Share Posted September 26, 2022 try <?php if (isset($_GET["leapyear"]) && !empty($_GET['leapyear'])) { $result = (is_leapyear($_GET["leapyear"])) ? "<span style=\"color:#008631;\">{$_GET['leapyear']} is a leap year</span>" : "<span style=\"color:#FF0000;\">{$_GET['leapyear']} is not a leap year</span>" ; } else $result = '' ; function is_leapyear($year){ if(is_numeric($year)){ if($year%4 == 0) { if($year%100 == 0) { return ($year%400 == 0); } else return true; } } return false; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Leap year form</title> </head> <body> <?= $result ?> <h1>Leap Year</h1> <form method = "get" > <label for="leapyear">Enter a year</label> <input type="text" name="leapyear" id="leapyear" autofocus/> <p><input type="submit" name="confirm" value="Check For Leap Year" /></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601056 Share on other sites More sharing options...
Bhekstar007 Posted September 26, 2022 Author Share Posted September 26, 2022 Thank you it works perfectly, if you don't mind may you explain the logic to me. Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601057 Share on other sites More sharing options...
Barand Posted September 26, 2022 Share Posted September 26, 2022 function is_leapyear($year){ if(is_numeric($year)){ if($year%4 == 0) { // if it's divisible by 4 its probably a leapyar if($year%100 == 0) { // but check the century condition return ($year%400 == 0); // if its divisible by 400 return true otherwise return false } else return true; } } return false; // if we get to here, return false } Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601061 Share on other sites More sharing options...
Bhekstar007 Posted September 27, 2022 Author Share Posted September 27, 2022 Ok thank you I now understand Quote Link to comment https://forums.phpfreaks.com/topic/315370-using-the-isset-function-in-php-i-want-to-display-a-confirmation-message-but-its-not-showing-properly/#findComment-1601095 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.