Hi there, I've not been on here in a while. I'm having trouble with a PHP function and I'm pretty sure I'm missing something very obvious or I'm doing something wrong so hopefully someone can help me reach that inevetable facepalm or headdesk when I realise what I'm doing wrong sooner.

I've created a very basic function to simply call the various parts of the page to display.

function content ($pagecontent)
{
    $content = "";

    $content .= include("/header.php");
    $content .= $pagecontent;
    $content .= include("footer.php");

    return $content;
}

This is only partly working however. The content from the includes are being placed in the variable and returned but for some reason, when I try to pass something in to the $pagecontent variable, it seems to come empty.

The $pagecontent variable contains what it says - the page content. An example page script using the function:

<?php
    $mypage = ((results from scripts grabbing content from database and formatting etc));

    content($mypage);
?>

If I echo the $mypage variable (between the two includes like the function) directly on the page, the content is displayed however it doesn't seem to want to pass itself to the function as when I call the function, passing the variable to it, only the includes are displayed. If I am not mistaken (which I must be looking at the results), the content of the variable should be passed to the function as above.

Any help or pointers would be appreciated.

Recommended Answers

All 2 Replies

You'll need to:

echo content($mypage);

because the new content is returned from the function. It doesn't output anything itself. If the includes are outputting, then there's no point in using the $content variable and returning it's value:

function content($pagecontent)
{
    include 'header.php';
    echo $pagecontent;
    include 'footer.php';
}

And there's the headdesk. That worked without problem.

Thanks for that. I can't believe I missed something so obvious.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.