php - 函数的含义

标签 php c function variables operators

这是我不理解的功能:

$year = date("Y");
$leapYear = false;
if((($year%4==0) && ($year%100==0)) || ($year%400==0))
    $leapYear = true;

这是否意味着,如果(($year 除以 4 但不除以 100) 或 (年份除以 400)) 就是闰年?

我是 PHP 新手,请任何人帮助我理解这些语句。

最佳答案

我认为您可能会感到困惑,因为您提供的代码找不到闰年。闰年可以被 4 整除,但不能被 100 整除..除非它们也可以被 400 整除。如果你翻转逻辑,就会更容易理解。如果它能被 400 整除,无论如何它都是闰年。否则,如果能被 4 整除且不能被 100 整除,则为闰年。

// pseudocode
if ( year % 400 == 0  || (year % 4 == 0 && year % 100 != 0))
    return true;
else
    return false;

你可以尝试一下,然后说:

return year % 400 == 0  || (year % 4 == 0 && year % 100 != 0)

&& 表示双方都必须为 true 才能让 && 返回 true

关于php - 函数的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667242/

相关文章:

php - "Apache started [Port 80] Busy..."- 此类错误表明

php - wp_redirect() 不工作并且不重定向到 wordpress 中的另一个页面

c - 使用 CUDA 5 的 cudaMemcpyToSymbol 出错

c++ - 为什么函数不能在 C 中有引用参数?

function - Powershell CommandNotFoundException

php - 两个日期相隔多少天?

php - Laravel 中的数据库关系

R:测试函数从封闭环境中获取哪些对象

c - 动态大小的指向链表的指针数组

c - 调试递归函数