Louisa Louisa - 1 year ago 126
PHP Question

PHP page level variables

Not much of a PHP programmer, so I have a quick question in order to improve the performance of a WP site.

For many pages, the header and the body are handled by a set of completely separate functions.

The body functions do a lot of the same work as the header functions have already done, so is it possible to save the results of the header functions in a set of page level variables? (to avoid doing the same work twice).

Page level variables = variables with a page level scope that separate functions on the same page all have read/write access to.


Answer Source

You're probably looking for global, which is described on this page: http://php.net/manual/en/language.variables.scope.php

Short example:

function do1()
    global $foo;
    $foo = do work ...

functio do2()
    global $foo;
    $bar = do work with ($foo); ...


And one word of adivce; be very careful not to accidentally reuse names for global variables.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download