-1

I have the code below on a page basically what I'm trying to do is fill $content variable using the function pagecontent. Anything inside pagecontent function should be added to the $content variable and then my theme system will take that $content and put it in theme. From the answers below it seems you guys think I want the html and php inside the actual function I don't.

This function below is for pagecontent and is what I'm currently trying to use to populate $content.

function pagecontent()
{
        return $pagecontent;
}

<?php

    //starts the pagecontent and anything inside should be inside the variable is what I want
    $content = pagecontent() {
?>

I want anything is this area whether it be PHP or HTML added to $content using pagecontent() function above.


<?php

     }///this ends pagecontent
    echo functional($content, 'Home');

?>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
leanswag
  • 53
  • 5
  • Im still learning to code so i dont know what that is ill google it now and hope you give me a respond on it aswell – leanswag Oct 28 '12 at 18:29
  • restructure the code. move the content in to he function. –  Oct 28 '12 at 18:29
  • I know it's not pertinent to your question, but you did include the code. If you can avoid it, please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the *[red box](http://goo.gl/GPmFd)*? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use [PDO](http://php.net/pdo). Please see [this article](http://goo.gl/3gqF9). [Here is good tutorial](http://goo.gl/vFWnC) for PDO. Also see [Why shouldn't I use `mysql` functions in PHP?](http://goo.gl/ycnmO) – vascowhite Oct 28 '12 at 18:30
  • 1
    You need to learn some basics first it looks like: http://php.net/functions - please do so before asking such questions, especially as you already put so much code there that is not necessary to create the picture of the problem you're facing. Your code by the way has a syntax error and does not work. I assume you're doing pseudo code here, but it might not be obvious by the wording of your quesiton. – hakre Oct 28 '12 at 18:30
  • Also you might be interested in this question: [Modify an Existing PHP Function to Return a String](http://stackoverflow.com/q/8730847/367456) – hakre Oct 28 '12 at 18:38
  • @vascowhite: As an aside, would you mind awfully using the full, proper URLs for documentation so that I can see where I'm going before I click on a link? Appreciate it. – Lightness Races in Orbit Oct 28 '12 at 18:39
  • @LightnessRacesinOrbit Unfortunately, I think using the full links in that comment will make it too long. I'll check and try though, seeing as you asked so nicely :) I appreciate your concern over shortened links. – vascowhite Oct 28 '12 at 18:41

3 Answers3

1

As you are obviously a beginner here's a much simplified, working version to get you started.

function pageContent()
{
    $html = '<h1>Added from pageContent function</h1>';
    $html .= '<p>Funky eh?</p>';
    return $html;
}

$content = pageContent();
echo $content;

The rest of the code you post is superfluous to your problem. Get the bare minimum working first then move on from there.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Not what i need tho I want $content = pageContent() { and anything in here } gets added to $content – leanswag Oct 28 '12 at 18:39
  • @leanswag See my edit, but it seems a wasted step. Your call though :) Also, my original version was more readable. – vascowhite Oct 28 '12 at 18:43
1

I think you're looking for output buffering.

<?

// Start output buffering
ob_start();

?> Do all your text here

<? echo 'Or even PHP output ?>
And some more, including <b>HTML</b>

<?

// Get the buffered content into your variable
$content = ob_get_contents();

// Clear the buffer.
ob_get_clean();

// Feed $content to whatever template engine.
echo functional($content, 'Home');
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Yeah it just grabs anything that's in its way. :) I wouldn't use output buffering too often. You'll get in trouble if you call code that uses this from other code that also uses it. Output buffering is something that doesn't nest well, making it riskful to use in larger, more complex websites. – GolezTrol Oct 28 '12 at 18:52
  • it works and im planning to use on lots of pages that will be a problem? – leanswag Oct 28 '12 at 18:54
  • Not per se, but it will if you decide to embed (include) such a page as part of another page that also uses it. If that is not the case, then you're probably safe. But if you build a website with lots of pages, read about content management systems like Drupal or frameworks like Yii. Especially the latter may be helpful and may help you in learning how to do proper, structured programming. – GolezTrol Oct 28 '12 at 19:13
1

Way 1:

function page_content(){
  ob_start(); ?>

    <h1>Hello World!</h1>

  <?php
  $buffer = ob_get_contents();
  ob_end_clean();
  return $buffer;
}

$content .= page_content();

Way 2:

function page_content( & $content ){
  ob_start(); ?>

    <h1>Hello World!</h1>

  <?php
  $buffer = ob_get_contents();
  ob_end_clean();
  $content .= $buffer;
}


$content = '';
page_content( $content );

Way 3:

function echo_page_content( $name = 'John Doe' ){
  return <<<END

    <h1>Hello $name!</h1>

END; }

echo_page_content( );
biziclop
  • 14,466
  • 3
  • 49
  • 65