当前位置:  开发笔记 > 编程语言 > 正文

为什么我的IDE将此变量标记为未使用?

如何解决《为什么我的IDE将此变量标记为未使用?》经验,为你挑选了1个好方法。

因为你不需要这条线

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

由于IDE告诉此变量将被此行立即覆盖:

$freeSlots = (5 - $startSlot) + ($endSlot - 1);

所以第一行实际上是一条完全没必要的行,可以删除.你永远不会使用$ freeSlots,你正在重新定义它.

澄清范围:

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

if (strtotime($endDate) === strtotime($startDate))
{
    return $endSlot - $startSlot;
}

$freeSlots = (5 - $startSlot) + ($endSlot - 1);
$newTime = strtotime('+1 day', $newTime);

if (date("Y-m-d", $newTime) === $endDate)
{
    return $freeSlots;
}

do
{
    if (!(date('N', $newTime) >= 6))
    {
        $freeSlots += 4;
    }
    $newTime = strtotime('+1 day', $newTime);
} while (date("Y-m-d", $newTime) !== $endDate);

return $freeSlots; //but is clearly used here

上面的代码与您的代码完全相同.你不需要使用else.如果条件为真,那么您将返回一个值.否则你继续.不需要别的.所以这是相同的范围.



1> KhorneHoly..:

因为你不需要这条线

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

由于IDE告诉此变量将被此行立即覆盖:

$freeSlots = (5 - $startSlot) + ($endSlot - 1);

所以第一行实际上是一条完全没必要的行,可以删除.你永远不会使用$ freeSlots,你正在重新定义它.

澄清范围:

$freeSlots = 0; //Unused local variable 'freeSlots'. The value of the variable is overwritten immediately.

if (strtotime($endDate) === strtotime($startDate))
{
    return $endSlot - $startSlot;
}

$freeSlots = (5 - $startSlot) + ($endSlot - 1);
$newTime = strtotime('+1 day', $newTime);

if (date("Y-m-d", $newTime) === $endDate)
{
    return $freeSlots;
}

do
{
    if (!(date('N', $newTime) >= 6))
    {
        $freeSlots += 4;
    }
    $newTime = strtotime('+1 day', $newTime);
} while (date("Y-m-d", $newTime) !== $endDate);

return $freeSlots; //but is clearly used here

上面的代码与您的代码完全相同.你不需要使用else.如果条件为真,那么您将返回一个值.否则你继续.不需要别的.所以这是相同的范围.


控制结构共享相同的范围
推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有