Ticks = bizarre

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Ticks = bizarre

Post 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();
?>
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Ticks = bizarre

Post 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();

?>
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Ticks = bizarre

Post 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.
Post Reply