PHP gets boolean when set later in the page

In the header of my page I have a conditional statement to check if the $ foo boolean is set, the problem is that the $ foo boolean is not set until the footer is loaded. Any way to retroactively check the status of this boolean?

+2


a source to share


4 answers


You can buffer the output of the footer, then generate the header and output it first.



ob_start();
outputFooter();
$footer= ob_get_clean();

outputHeader();
outputBody();
echo $footer;

      

+2


a source


You cannot retroactively check the status. Your best bet is to have the footer function run first and store the footer in a variable and then repeat it later.



Or, better, restructure the script to define all state first, then HTML is generated.

+3


a source


No. At least not an easy way. You will need to use a templating system that can fire the output after the page has finished loading.

0


a source


No. Just a few alternatives, try refactoring your code to make sure the value is set first. Use session to set values ​​might also be an option.

0


a source







All Articles