简体   繁体   中英

PHP stop function return from being overwritten

I've created two functions:

function script_start_time()
{
    $start_time = microtime(TRUE);
    return $start_time;
}

and

function script_end_time()
{   $start = script_start_time();
    $end_time = microtime(TRUE);
    $time_taken = $end_time - $start;
    $time_taken = round($time_taken, 4);
    echo 'Page generated in '.$time_taken.' seconds.';
}

function script_start_time() is called in header.php and function script_end_time is called in footer.php

I am trying to use $start_time value in function script_end_time without resorting to using global or static . Currently I get 0 in the echo, which I know is incorrect.

Where am I going wrong with these functions as it seems like $start is getting overwritten in function script_end_time instead of retaining the value from function script_start_time .

The problem is your script_start_time function never saves the value of $start_time and instead always overwrites it anytime the function is called. So you're effectively asking for the current time anytime you call the function.

If you adjust your code slightly then it should work as expected:

function script_start_time()
{
    // SAVE the value of $start_time the first time it is requested
    static $start_time;
    if (empty($start_time))
    {
        $start_time = microtime(TRUE);
    }
    return $start_time;
}

function script_end_time()
{   
    $start = script_start_time();
    $end_time = microtime(TRUE);
    $time_taken = $end_time - $start;
    $time_taken = round($time_taken, 4);
    echo 'Page generated in '.$time_taken.' seconds.';
}

script_start_time();

usleep(5000);

script_end_time(); // Page generated in 0.0052 seconds.

Example: https://eval.in/861112

function script_start_time()
{
    return microtime(TRUE);
}

function script_end_time($start_time)
{   
    $end_time = microtime(TRUE);
    $time_taken = $end_time - $start_time;
    $time_taken = round($time_taken, 4);
    echo 'Page generated in '.$time_taken.' seconds.';
}

$start_time = script_start_time();

usleep(4000);

script_end_time($start_time);

Rather than use the same function to save the start time and retrieve it, you could use a class:

class Timer {
    private static $start_time;

    public static function start() {
        self::$start_time = microtime(TRUE);
    }

    public static function show_time_since_start() {
        $end_time = microtime(TRUE);
        $time_taken = $end_time - self::$start_time;
        $time_taken = round($time_taken, 4);
        echo 'Page generated in '.$time_taken.' seconds.';
    }
}

Then at the beginning of the script you call:

Timer::start();

and at the end you do:

Timer::show_time_since_start();

Actually, I wouldn't put the echo statement in the function. I would just make a function that returns $time_taken , and do the printing in the caller.

And instead of just one timer, you could change the class so $start_time is an object property rather than a class property, and you could then create multiple timers that each remember a different start time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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