Page 1 of 1

Ticks = bizarre

Posted: Tue Oct 26, 2004 7:24 pm
by Roja
Okay, this script works as expected - if you run it *once*.

If, however, you hit refresh, the array ($duh) gets longer and longer - as if DESPITE the fact that we clear the array at the beginning, it has a "memory" of it.

This is on php-4.3.8 (fedora core2), with register_globals OFF.

Why does the count of $duh get longer with each refresh, and why doesnt setting it to a blank string clear it?

I've never used ticks before, and this behavior seems to defy normal behavior..

Code: Select all

<?php
echo "<html><body>";
// A function that records the time when it is called

$duh[] = '';
var_dump($duh);

function profile ($dump = FALSE)
{
    static $duh;

    // Return the times stored in profile, then erase it
    if ($dump)
    {
        return ($duh);
    }
    else
    {
        $duh[] = microtime ();
    }
}

// Set up a tick handler
register_tick_function("profile");

// Initialize the function before the declare block
profile();

// Run a block of code, throw a tick every statement.
declare (ticks=1)
{
    echo ".";
}

// Display the data stored in the profiler
echo "<br><pre>";
print_r (profile (TRUE));
echo "</pre></body></html>";

// Try to kill the phantom values..
unset($duh);
unregister_tick_function("profile");
die();
?>

Posted: Tue Oct 26, 2004 8:30 pm
by Roja
Even easier testcase..

Code: Select all

<?php
echo "<html><body>";

function haha()
{
    echo "!";
}

// Set up a tick handler
register_tick_function("haha");

// Run a block of code, throw a tick every statement.
declare (ticks=1)
{
    echo ".";
}

unregister_tick_function("haha");

echo "</body></html>";

// Try to kill the phantom values..
die();
?>
Rerunning it gets successively longer strings of !!!!!!!!!...

How?

Re: Ticks = bizarre

Posted: Tue Oct 26, 2004 10:54 pm
by timvw
Roja wrote: $duh[] = 'foo';
var_dump($duh);

function profile ($dump = FALSE)
{
static $duh;
echo $duh;
}
the first $duh is not the same as the $duh inside the function.

Code: Select all

<?php

$duh = 'foo';
echo "$duh <br>";

function bar()
{
    static $duh;
    $duh .= "!";
    echo "$duh <br>";
}

bar();
bar();
bar();

?>

Re: Ticks = bizarre

Posted: Wed Oct 27, 2004 12:01 am
by Roja
timvw wrote: the first $duh is not the same as the $duh inside the function.
Thats why I provided the second test case - no variables at all in the second testcase, and it still does the same "memory of previous callbacks" oddness.