PHP Ternary Operator for Leap Years
You can use the PHP ternary operator to determine if a year is a leap year. A leap year is defined as being divisible by 4 but not by 100, or divisible by 400.
Here is an example code in PHP using the ternary operator to determine if a year is a leap year.
<?php
$year = 2020;
$isLeapYear = (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) ? "是闰年" : "不是闰年";
echo $year . $isLeapYear;
?>
In the code above, a variable $year representing the year is first defined. Then a ternary operator is used to determine if the year is a leap year, and the result is assigned to the variable $isLeapYear. If it is a leap year, the value of $isLeapYear is “is leap year”, otherwise it is “not a leap year”. Finally, the result is output using the echo statement. In this example, the output is “2020 is a leap year.”