Setting variable in header.php but not seen in footer.php

In wordpress , I set a variable in header.php
$var= 'anything';
but in footer.php when I echo it
echo $var;
I got no thing printed. why?
Solution for : setting variable in header.php but not seen in footer.php

You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

So just declare your variable as global:
$GLOBALS[ 'var' ] = '...';
Then you can echo it globally.
echo $GLOBALS[ 'var' ];

No comments:

Post a Comment