hukadeeze Posted May 15, 2007 Share Posted May 15, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/51415-solved-executing-php-inside-a-return-statement/ Share on other sites More sharing options...
stevesimo Posted May 15, 2007 Share Posted May 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51415-solved-executing-php-inside-a-return-statement/#findComment-253204 Share on other sites More sharing options...
hukadeeze Posted May 15, 2007 Author Share Posted May 15, 2007 There is only one return statement. I'm trying to return a string of html and executed script rather than a single value. Quote Link to comment https://forums.phpfreaks.com/topic/51415-solved-executing-php-inside-a-return-statement/#findComment-253210 Share on other sites More sharing options...
btherl Posted May 15, 2007 Share Posted May 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51415-solved-executing-php-inside-a-return-statement/#findComment-253226 Share on other sites More sharing options...
hukadeeze Posted May 15, 2007 Author Share Posted May 15, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/51415-solved-executing-php-inside-a-return-statement/#findComment-253245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.