1

Sorry if this sounds stupid! I'm using PHP to create certain sections of my website like the header, then echoing them on to every page. The problem is I want to put some PHP code within the section like this:

<?php
function makeHeader () {
$menuContent = <<<MENU

<!DOCTYPE html>
<html> etc..........


<?php
if(isset($_SESSION['user']))
    {
            $user = isset($_SESSION['user']) ? $_SESSION['user'] : '';  
            echo "<a href='logout.php'>$user</a>";
        }
    else
        {
            echo "some text..";
        }
    ?> 

MENU;
$menuContent .= "\n";
return $menuContent;
}

?>

Is this possible? Its throwing up errors but starting from the if(isset($_SESSION['user'])) line rather than the line above it..

6 Answers 6

2

Use single or double quoted string instead of heredoc and it should be fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Or use an include. But yeah - generally I'm against HEREDOC syntax. I've never came across a single situation where it's been the right thing to use
@kissmyface Well, if we have to declare a string with numerous single and double quotes in it, heredoc does make things easier, although not a necessity. ;)
I would argue that if you need to do that then you are 'doing it wrong', though I do appreciate that sometimes we have to work on other peoples code. sometimes that code is very bad. and sometimes we need to just 'get things done' :-)
1

The error you are getting (but not mentioned) is probably a parse error. HEREDOC syntax requires {} curly braces around variables, or no key quotes for simple array accesses. The workaround would be a NOWDOC string.


But you can't do it with that method anyway. PHP code in string context doesn't run. And it's pointless to apply any workarounds. Just use an include script instead of a HEREDOC and use ob_start and ob_get_clean for capturing the output.

Comments

0

What is the errors?

Do you have session_start beforehand? Eg.:

function makeHeader () {  
session_start();
$menuContent = <<<MENU 

Comments

0

I don't think that's possible, but I would be very interested to learn if I'm wrong.

As for what you're doing here, I think your life will be a million times easier if you just dump the PHP content for your header ing a file called header.php and include it like this:

<?php

  include("header.php"); // Containing the code you're trying to put into $menuContent

?>

There are also plenty of good templating frameworks (Smarty, Liquid, Mustache) out there if you want to avoid reinventing this wheel, though I understand the appeal.

Comments

0

Try this:

function makeHeader () 
{
    $menuContent = "<!DOCTYPE html><html> etc.........." . 
    if(isset($_SESSION['user']))
    {
        $user = isset($_SESSION['user']) ? $_SESSION['user'] : '';  
        $menuContent .= "<a href='logout.php'>$user</a>";
    }
    else
    {
        $menuContent .= "some text..";
    }

    $menuContent .= "\n";
    return $menuContent;

}

1 Comment

You might need to recheck the code, but i hope u get the idea
0

is your code partial? or are you missing a closing } at the end of the function makeHeader? that would throw an exception on the "if" line. not to mention the missing closing php tag before the HTML up top.

I think the real issue is that you can't have and end tag and HTML within a function. if you REALLY need this then you can "echo" the HTML if needed. but I can't imagine you would want to write the entire doc from start to finish with echo statements.

would the following be suitable?

<?PHP
    session_start();
    $menuContent = <<<MENU;
?>
<!DOCTYPE html>
<html>
    <body>
    <?PHP
        echo $menuContent;
        if(isset($_SESSION['user'])) {
            $user = isset($_SESSION['user']) ? $_SESSION['user'] : '';  
            echo "<a href='logout.php'>$user</a>";
        } else {
            echo "some text..";
        }
    ?>
    etc..........
    </body>
</html>

or do you need to build it all in the menu section before?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.