Jump to content

Order of statements - workaround please


dbl07

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.