如果满足条件,PHP 将为变量分配新值


php assign new value to variable if condition is met

下面的代码中有什么更好的方法可以向变量添加新值$timestop$time_diff是否满足条件?

//Calculates difference in time using 24h format
$timestart = strtotime("14:00:00");
$timestop = strtotime("07:00:00"); //if smaller value, it must end next day and meets the condition below
$time_diff = $timestop - $timestart; //elapsed time
if ($time_diff < 0 || $time_diff == 0) //if result is negative value, $timestop ends next day
{
   $timestop = strtotime( '+1 day' , strtotime("07:00:00") ); //+ 1 day changes timestamp
}
/* UPDATED */
$time_diff = $timestop - $timestart; //added again
echo $time_diff;

不,覆盖变量没有问题,这是日常工作。您甚至没有覆盖任何其他类型,您只是重置为另一个整数值。

它甚至更好,因为您不会浪费任何额外的内存空间(当然不高,但要大规模考虑)。