0

I don't know if the title is correct, anyway here is what I need:

This is my code:

$start = microtime();
$string = $_POST['string'];
$matches = $SQL->prepare("SELECT * FROM `users` WHERE `name` LIKE ?");
$matches->execute(array('%'.$string.'%'));

echo "Done in: " . round($_GLOBALS['time'], 4);
foreach($matches->fetchAll() as $match) {
    [..]
}
$end = microtime();
$time = $end - $start;

As you can see, I'm measuring the time of the query + displaying records, microtime() needs to be at the bottom of the foreach but I need to display the measured time ($time variable) before a foreach. As you can see I've tried to do this like this: echo "Done in: " . round($_GLOBALS['time'], 4); but it always is returning 0 (null).

What should I do?

afuzzyllama
  • 6,275
  • 5
  • 42
  • 61
Rym
  • 77
  • 1
  • 1
  • 7
  • 2
    So far, there's no time travel in allowed in PHP. – Jared Farrish Oct 09 '11 at 15:58
  • Sorry, I really couldn't understand what you're trying to achieve. You want to display the time of the query execution before the foreach, and the time of record displaying as well after the foreach? – Alessandro Desantis Oct 09 '11 at 15:59
  • Why do you need to print the `Done in` statement *before* the loop? – Jared Farrish Oct 09 '11 at 15:59
  • Sense. This question makes none. You can't possibly calculate the time it takes for you to perform an action *before* you perform said action. – NullUserException Oct 09 '11 at 16:00
  • @JaredFarrish Because after the loop, there'll be much table rows, and user will have to scroll down, to see execution time. Thats why I want it be to placed at the top of the page. – Rym Oct 09 '11 at 16:02
  • Then don't use `echo`'s to print directly to the output buffer. Take the results and put them into a variables (including the table), or alternatively use [output buffering](http://php.net/manual/en/book.outcontrol.php), *then* output the variables/output buffer. – Jared Farrish Oct 09 '11 at 16:04

3 Answers3

1

You should do something like this:

$start = microtime(true);
$string = $_POST['string'];
$matches = $SQL->prepare("SELECT * FROM `users` WHERE `name` LIKE ?");
$matches->execute(array('%'.$string.'%'));

$output = '';
foreach($matches->fetchAll() as $match) {
    $output .= $someText;
    [...]
}

$end = microtime(true);
$time = $end - $start;
echo "Done in: " . round($time, 4);
echo $output;

Also, note I am using the optional parameter of true because this returns the time as a decimal number instead of two parts as the documentation states.

afuzzyllama
  • 6,275
  • 5
  • 42
  • 61
0

afuzzyllama's answer is one way using a variable. You could also capture the foreach loop's output into a buffer, echo the time, then output from the buffer:

$start = microtime();
$string = $_POST['string'];
$matches = $SQL->prepare("SELECT * FROM `users` WHERE `name` LIKE ?");
$matches->execute(array('%'.$string.'%'));

ob_start();
foreach($matches->fetchAll() as $match) {
    [..]
}
$table = ob_get_contents();
ob_end_clean();

$end = microtime();
$time = $end - $start;
echo "Done in: " . round($GLOBALS['time'], 4);
echo $table;

Also, I don't know why you're using $GLOBALS['time'] here.

Jared Farrish
  • 46,034
  • 16
  • 88
  • 98
-1

Can't you just calculate the time using microtime before the foreach too? e.g.

$start = microtime();
$string = $_POST['string'];
$matches = $SQL->prepare("SELECT * FROM `users` WHERE `name` LIKE ?");
$matches->execute(array('%'.$string.'%'));

echo "Done in: " . (microtime() - $start);
foreach($matches->fetchAll() as $match) {
    [..]
}
$end = microtime();
$time = $end - $start;
Nick Shaw
  • 2,043
  • 18
  • 25