• Resolved saphod

    (@saphod)


    Hi folks!

    I tried to put some php-code into the header and footer to calculate and print the loading time of a page.

    On the first line of the header.php, I inserted

    <?php $pageload_start = getmicrotime(); ?>

    where getmicrotime() is a funtion that returns the seconds and microseconds as a float and is created in the functions.php of the template.

    In the footer.php, I inserted the following code:

    <php
    $pageload_end = getmicrotime();
    $pageload_time = round($pageload_end - $pageload_start,3);
    echo $pageload_time;
    ?>

    The problem is:
    Only the value of $pageload_end will be remembered, it seems as if $pageload_start never existed! So, the time printed is actually the time when $pageload_end was defined and not the loading time.

    I know this must be a php feature, so I tried to set $pageload_start as a global variable:

    <?php
    $pageload_start = getmicrotime();
    global $pageload_start;
    ?>

    Didn’t work, eiter.
    What am I missing?

    Remember: I am quite a php newbie. ??

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter saphod

    (@saphod)

    C’mon, guys, this can’t be too hard for you, can it? ??

    ahem…

    <?php
    global $pageload_start;
    $pageload_start = getmicrotime();
    ?>

    Global variables must be scoped first (to global).

    Thread Starter saphod

    (@saphod)

    That is right, I doubted this, too. I think I have seen it somewhere else and may have copied it in that wrong way… BUT: if I change the order, there is no difference.

    I am proud to announce that:
    I found the answer!

    When you put this into the footer, it works (without the arrows, of course):

    <?php
    --> global $pageload_start; <--
    $pageload_end = getmicrotime();
    $pageload_time = round($pageload_end - $pageload_start,3);
    echo $pageload_time;
    ?>

    Notice that I missed a ? in the post before, it must of course be <?php.

    Redefining the variable as global in the footer actually did the trick. Hm, I thought it would be enough to define it just once as global, but it seems that you have to do it in the header as well as in the footer… otherwise, it won’t work (tried it).

    Thanks for your help, nonetheless. ??

    Funny… I always thought that variables only have to be set as global if you were using functions… but I am not using $pageload_start in a function, am I? The header and footer are put together to one page in the end, so why does this still only work when defining it as global?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show page loading time in footer?’ is closed to new replies.