简体   繁体   English

php echo函数不打印结果

[英]php echo function not printing result

I have a script to count the time a ticket is open each day, but only for hours we are open.我有一个脚本来计算每天门票打开的时间,但只有我们开放的几个小时。 The details of the functions arent that important but i have pasted it all here as it may be the cause for the failure.功能的细节并不那么重要,但我已将其全部粘贴在这里,因为它可能是失败的原因。

Heres the function:这里的功能:

function getTime($o, $now, $total) {

    //One Day in seconds
    $oneDay = 86400;

    //One Business day (12 hours, 7am-7pm)
    $oneBDay = 43200;

    //Get the timestamp of 7am/7pm for time given

    $sevenAM = mktime('7', '0', '0', m($o), d($o), y($o));
    $sevenPM = mktime('19', '0', '0', m($o), d($o), y($o));

    //If Ticket Start Time is before 7am, we only count from 7am after
    if ($o < $sevenAM) {
        $o = $sevenAM;
    } else {
        $o = $o;
    }

    //Debug to get today
    $today = date('Y-m-d h:i:s a', $o);

    //See if we are within the same business day
    $diff = $now - $o;

    //Debug
    //echo $today.",".$timeSpent.",".$total."\n";

    //If we are not within 1 business day, do this again
    if ($diff > $oneBDay) {

        //Total Time spent for the day
        $timeSpent = $sevenPM - $o;

        //Add todays time to total time
        $total = $total + $timeSpent;

        //Move to tomorrow
        $o = $sevenAM + $oneDay;

        getTime($o, $now, $total);
    }

    //If we are within 1 business day, count the time for today and return our result
    if ($diff < $oneBDay) {
        $time = $diff;
        $total = $total + $time; //for example $total = 123456
                    return $total;
            }
}

when I do当我做

echo getTime($o,$now,0); 

I would expect to see 123456. But i get nothing printed.我希望看到 123456。但我什么也没打印。

The function runs and I know total has a value ( I have set it statically as a debug).该函数运行并且我知道 total 有一个值(我已将其静态设置为调试)。

--Note the function calls itself if needed --注意函数在需要时调用自身

additional functions:附加功能:

function y($o){

    $y = date('Y',$o);
    return $y;
}
function m($o){
    $m = date('m',$o);
    return $m;
}
function d($o){
    $d = date('d',$o);
    return $d;
}

EDIT:编辑:

if i do :如果我做 :

        if ($diff < $oneBDay) {
        $time = $diff;
        $total = $total + $time; //for example $total = 123456
        echo "My Total:".$total;
                    return $total;
            }

I will see My Total:123456我会看到我的总数:123456

It's not in vain to point out that your function is hard to debug due to structural flaws.指出您的函数由于结构缺陷而难以调试并不是徒劳的。 I suggest you to create a Unit Test for your function and then refactor so you will be able to make sure that you preserve the desired behaviour.我建议您为您的函数创建一个单元测试,然后重构,以便您能够确保保留所需的行为。

That said, your function is printing anything because it's not reaching any explicit return directive.也就是说,您的函数正在打印任何内容,因为它没有达到任何显式return指令。 So it returns null .所以它返回null See that if the last if is not hit, you will miss the last chance for returning a value.请注意,如果最后一个 if 没有命中,您将错过最后一次返回值的机会。

I'm not sure what your function should do, but it looks like if you call it recursively, you may want to return it's value, or at least reassign to a variable.我不确定你的函数应该做什么,但看起来如果你递归调用它,你可能想要返回它的值,或者至少重新分配给一个变量。 Checkout for that.结帐。

<?php

function y($o){ $y = date('Y',$o); return $y; }
function m($o){ $m = date('m',$o); return $m; }
function d($o){ $d = date('d',$o); return $d; }


function getTime($o,$now,$total){

//One Day in seconds
$oneDay = 86400;

//One Business day (12 hours, 7am-7pm)
$oneBDay = 43200;

//Get the timestamp of 7am/7pm for time given

    $sevenAM = mktime('7','0','0',m($o),d($o),y($o));
    $sevenPM = mktime('19','0','0',m($o),d($o),y($o));

//If Ticket Start Time is before 7am, we only count from 7am after
    if ($o < $sevenAM){
            $o = $sevenAM;
    }else{
            $o = $o;
    }

//Debug to get today
    $today = date('Y-m-d h:i:s a',$o);

//See if we are within the same business day
    $diff = $now - $o;

//Debug
    //echo $today.",".$timeSpent.",".$total."\n";

//If we are not within 1 business day, do this again
            if ($diff > $oneBDay){

                    //Total Time spent for the day
                    $timeSpent = $sevenPM - $o;

                    //Add todays time to total time
                    $total = $total+$timeSpent;

                    //Move to tomorrow
                    $o = $sevenAM+$oneDay;

                    return getTime($o,$now,$total); // LOOKS LIKE YOU WANT TO RETURN VALUE HERE
            }

//If we are within 1 business day, count the time for today and return our result
            if($diff < $oneBDay){
                    $time = $diff;
                    $total = $total+$time; // FIXED MISSING SEMICOLON HERE TO AVOID SYNTAX ERROR
                    return $total;
            }
 }


$test = getTime(1534964212, date('U'), 0);

echo "$test"; // 144885

?>

Although I'm not exactly sure what you tried to do with this function, I've corrected a few syntax errors, and added some returns for cases that were missing.尽管我不确定您尝试使用此函数做什么,但我已更正了一些语法错误,并为丢失的情况添加了一些返回值。 (inside if ($diff > $oneBDay) { and changed if ($diff < $oneBDay) { to if ($diff <= $oneBDay) { ) (在if ($diff > $oneBDay) { ,并将if ($diff < $oneBDay) {更改为if ($diff <= $oneBDay) {

<?php

    function getTime($o, $now, $total) {

        //One Day in seconds
        $oneDay = 86400;

        //One Business day (12 hours, 7am-7pm)
        $oneBDay = 43200;

        //Get the timestamp of 7am/7pm for time given

        $sevenAM = mktime('7', '0', '0', m($o), d($o), y($o));
        $sevenPM = mktime('19', '0', '0', m($o), d($o), y($o));

        //If Ticket Start Time is before 7am, we only count from 7am after
        if ($o < $sevenAM) {
            $o = $sevenAM;
        }

        //See if we are within the same business day
        $diff = $now - $o;

        //If we are not within 1 business day, do this again
        if ($diff > $oneBDay) {

            //Total Time spent for the day
            $timeSpent = $sevenPM - $o;

            //Add todays time to total time
            $total = $total + $timeSpent;

            //Move to tomorrow
            $o = $sevenAM + $oneDay;

            return getTime($o, $now, $total);
        }

        //If we are within 1 business day, count the time for today and return our result
        if ($diff <= $oneBDay) {
            $time = $diff;
            $total = $total + $time; //for example $total = 123456;
            return $total;
        }
    }

    function y($o){ $y = date('Y',$o); return $y; }

    function m($o){ $y = date('m',$o); return $y; }

    function d($o){ $y = date('d',$o); return $y; }

    $o = 1534964212;
    $now = date('U');

    echo getTime($o,$now,0);

now it returns something like 145111现在它返回类似145111

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

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