19c03-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 68 行

PHP
68
字号
<?php// Declare a global array that we will be using to store this data$_timer_results = array();// A function that will add a new timing result to the globalfunction _timer() {	global $_timer_results;		// Immediately grab the time in microseconds	$curtime = microtime(true);		// Grab a backtrace so we can see who called this:	$trace = debug_backtrace();	// Now, the [0] entry refers to 'right now' in the backtrace, so we will	// use that to determine the filename and line #.  But will look	// at the [1] entry if it exists, for the calling function name.	$_timer_results[] = array(		'line' => $trace[0]['line'],		'file' => $trace[0]['file'],		'func' => isset($trace[1]['function']) ? $trace[1]['function'] : '',		'time' => $curtime		);}// Now immediately call timer once, to always place a timer at the entry// point of our script for us:_timer();// Now create a function that will turn these results into a readable text// string.  It will return this so that it can be dealt with as the program// needs, either via displaying, adding as an HTML comment, or whatever.function _timer_text() {	global $_timer_results;	$result = 'Timing Results';	// Start our rolling clock at the timestamp of the first entry	$clock = @$_timer_results[0]['time'];		// Now, loop through all entries in the timer results to create text	foreach ($_timer_results as $tr) {		// Calculate how long this one took, from the rolling clock		$thistime = $tr['time'] - $clock;				// Reset the clock for the next loop iteration		$clock = $tr['time'];				// Grab just the filename of the file:		$fn = basename($tr['file']);				// And to make output prettier, since we don't really need		// an extreme level of detail on the timing, let's convert		// the time to use 5 precision points:		$pretty = number_format($thistime, 5);				// Now make and add the string to the results		$result .= "\n{$pretty} secs - File: {$fn} - Line: {$tr['line']}";				// If there was a calling function, add it:		if ($tr['func']) {			$result .= " - Calling Function: {$tr['func']}";		}	}	// Return what we have created	return $result;}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?