How to pass code from header.php to footer.php

Avoid global variables, they could be overwritten by other code. You could use a helper function with a static internal variable instead. Sample code:

function wpse_69365_var_storage( $var = NULL )
{
    static $internal;

    if ( NULL !== $var )
    {
        $internal = $var;
    }

    return $internal;
}

// first call:
wpse_69365_var_storage( 4 );

// later call:
echo wpse_69365_var_storage(); // print 4

Leave a Comment