简体   繁体   English

PHP,相同的数据,相同的计算,不同的结果

[英]php, same data, same calculations, different results

I have the following code. 我有以下代码。 At the end I put in comments the result I have on the server. 最后,我在服务器上输入了结果。 Hope someone could explain me why the results are different, despite the calculations are the same. 希望有人能解释我为什么结果不同,尽管计算结果相同。

<?php 
date_default_timezone_set('UTC'); 

function formatHourToTime($input){ 
    if (strpos($input, '.') !== false){ 
            $array = explode(".",$input); 
    } 
    elseif (strpos($input, ':') !== false){ 
            $array = explode(":",$input); 
    } 
    elseif (strpos($input, ',') !== false){ 
            $array = explode(",",$input); 
    } 
    elseif ($input >= '0' & $input < '24'){ 
            $array = array($input); 
    } 
    else { 
            $time = false; 
            exit(); 
    } 
    $time = $array[0]*3600+$array[1]*60+$array[2]; 
    return $time; 
} 

$matin_d = 0; //midnight timestamp 0.00 
$matin_f = 10800; //ts de 3h00 
$soir_d = 79200; //ts de 22h00 
$soir_f = 82799; //ts de 23h59:59 

function nightwork($start, $end){ 

    if ($start < $matin_f && $end > $soir_d) $totalheures = ($matin_f - $start)/2 + ($end - $soir_d)/2+100000; 
    elseif ($start < $matin_f && $end < $matin_f) $totalheures = ($end - $start)/2+200000; 
    elseif ($start >= $soir_d && $end > $soir_d) $totalheures = ($end - $start)/2+300000; 
    elseif ($start < $matin_f) $totalheures = ($matin_f-$start)/2+400000; 
    elseif ($end>$soir_d) $totalheures = ($end-$soir_d)/2+500000; 
    else $totalheures = 0+600000;  

    return $totalheures; 
} 

$start = formatHourToTime('07:39:00')*1; 

$end = formatHourToTime('08:00:00')*1; 
$shiftnw = nightwork($start, $end); 
if($start >= $soir_d && $end > $soir_f) $bool = 'true'; 
else $bool = 'false'; 
//même code que la fonction nightwork 
    if ($start < $matin_f && $end > $soir_d) $totalheures = ($matin_f - $start)/2 + ($end - $soir_d)/2+100000; 
    elseif ($start < $matin_f && $end < $matin_f) $totalheures = ($end - $start)/2+200000; 
    elseif ($start >= $soir_d && $end > $soir_d) $totalheures = ($end - $start)/2+300000; 
    elseif ($start < $matin_f) $totalheures = ($matin_f-$start)/2+400000; 
    elseif ($end>$soir_d) $totalheures = ($end-$soir_d)/2+500000; 
    else $totalheures = 0+600000;  

echo $start.' '.$end.'<br>'; 
echo $totalheures. ' ' .$shiftnw;  
//$totalheures is calculated following the script
//while $shiftnw is calculated by calling the function having the same lines 
// prints : 
// 27540 28800 
// 600000 300630 
?>

It appears the problem is that within the nightwork() function you do not have access to these variables: 似乎出现的问题是,在nightwork()函数中,您无法访问以下变量:

$matin_d = 0; //midnight timestamp 0.00 
$matin_f = 10800; //ts de 3h00 
$soir_d = 79200; //ts de 22h00 
$soir_f = 82799; //ts de 23h59:59

Whereas you do have access to them in the global scope as calculated at the end of your script. 而您确实可以在脚本末尾计算出的全局范围内访问它们。

You would need to either place these values in the function as well, pass them to the function as parameters, declare them as global inside the function, or define them as constants to where they are available to all areas of your script regardless of scope. 您可能需要将这些值也放置在函数中,将它们作为参数传递给函数,将它们声明为函数内部的global变量,或者将它们定义为常量,以使它们对于脚本的所有区域(无论作用域)都可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM