I've written a php code that accepts month from user and returns number of days in that month. This is the php code:
<?php
// function to check leap year
function is_leap_year($year) {
return (( ($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0) ));
}
//array of month and day
$month_day = [
'jan' => 30,
'mar' => 30,
'apr' => 31,
'may' => 30,
'jun' => 31
];
//store current year in a variable
$current_year = date('Y');
//if current year is leap year
if (is_leap_year($current_year)) {
//then, 29 days for february
$month_day['feb'] = 29;
}
else {
//else, 28 days for feb
$month_day['feb'] = 28;
}
?>
<!-- html forn -->
<form action="">
<select>
<?php
//looping into array to create option tag
foreach ($month_day as $month => $days) {
echo "<option>$month</option>";
}
?>
</select>
<input type="submit">
</form>
the output of that code is this: picture showing output of the above code
but i need output like this: picture showing the required output
.
how to assign second order to feb element of the $month_day
array? If its not possible, then how to use that if condition as ternary operator inside array? I've seen some threads in this site regarding 'using if inside array' but it gives me error when i used. I appreciate if you answer with code showing just array section with ternary operator used.