Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/216920-order-of-statements-workaround-please/
Share on other sites

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.