Jump to content

[SOLVED] Executing php inside a return statement?


hukadeeze

Recommended Posts

class ProductsDisplay{

function product_contents($queryresults){
return 'html and php markup to output query'; <---this returns all markup as regular text.
return require (htmlandphpmarkupincludefile.php); <----this executes correctly, but the results are displayed before the rest of the template.
}
}

 

How can I return html with embedded php script that has already been executed?

Link to comment
Share on other sites

Im not quite sure what it is you are trying to do but I am fairly certain that you can only have 1 return statement in a function.  As soon as a script executes a return statement, the corresponding value is returned to the place that originally called the function.  In other words having two return statements one after the other wouldnt work as only the first one would ever get executed. 

 

I might be wrong but I know this is the case with Javascript and ASP.

Link to comment
Share on other sites

The reason it doesn't work with require() is that require() actually produces the output.  Instead, you will need to return instructions on doing the output, something like this:

 

function foo() {
  return 'htmlandphpmarkupincludefile.php';
}
$phpinclude = foo();
require($phpinclude);

 

Because require() appears outside the function, the display will occur outside the function.  If you put require() inside, even in the return statement, the output will be produced inside the function.

 

An alternative is using output buffering to catch the output.. it is much the same, but instead of returning the name of the included file, you will catch the output of the included file and return the output as a string, to be displayed later.

Link to comment
Share on other sites

I just got your response, but after I answered my own question. Instead of using the buffer I used the combo assign operator to add everything I needed to one variable and then returned the variable.

 


...
$var .= '<p>'.$title.'</p><br />';
$var .= '<p>'.$description.'</p><br />';
....

return $var;

Link to comment
Share on other sites

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.