dbl07 Posted October 26, 2010 Share Posted October 26, 2010 I know this has been addressed in tutorials and forums, but I'm not sure how to search for it (i.e. what to type into google). I have a header.php that I include in all files. It has a section like the following: if($showHello) { echo "Hello"; } I would like to control to header in the body of the other pages: $showHello = true; From what I can tell it is a problem with the order in which the page is processed: $showHello = true; if($showHello) { echo "Hello"; } // works whereas: if($showHello) { echo "Hello"; } $showHello = true; // does not work The reason for this makes sense, but my question is... how do I make the second scenario work? Is there a structure within PHP that I can use? Quote Link to comment https://forums.phpfreaks.com/topic/216920-order-of-statements-workaround-please/ Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 you have to set the value of the variable if you expect it to be set. you can't (easily) change your mind and "go back in time" to undo the conditional. Quote Link to comment https://forums.phpfreaks.com/topic/216920-order-of-statements-workaround-please/#findComment-1126846 Share on other sites More sharing options...
Psycho Posted October 26, 2010 Share Posted October 26, 2010 There is no one/right answer to your question - it all depends on exactly how the code is to be used. But, for illustrative purposes, let's say that in this example the "Hello" will be displayed at the top of the page, but you won't know whether it should be displayed until after you have processed all of the code for the rest of the page. Here is ONE example <?php $showHello = false; function getHelloText($showHelloBool) { return ($showHelloBool) ? 'Hello' : ''; } if($thisUser=='NewUser') { $content = "Welcome."; $showHello = true; } else { $content = "Welcome back."; } $helloText = getHelloText($showHello); ?><html> <body> <?php echo $helloText; ?> <?php echo $content; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/216920-order-of-statements-workaround-please/#findComment-1126851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.