jdrjsbl Posted June 15, 2011 Share Posted June 15, 2011 Hello, New guy here (sorry), I'm trying this but does not work: <?php $foo='<html> <head> <title>some title</title> </head> <body> <table> <tr><td> some text '.<?php include("file.php"); ?>.' some more text </tr></td> </table> </body> </html>'; echo $foo; ?> I want to display file.php in between some html code. Please help. I've spent about two hours looking but nothing. Thank you in advance, Jesse Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/ Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Try file_get_contents() instead of include(). <?php $foo='<html> <head> <title>some title</title> </head> <body> <table> <tr><td> some text ' . file_get_contents("file.php") . ' some more text </tr></td> </table> </body> </html>'; echo $foo; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229820 Share on other sites More sharing options...
jdrjsbl Posted June 15, 2011 Author Share Posted June 15, 2011 Thanks Redixx for the rapid response. I've tried your suggestion, just that it outputs everything including to top secret php code on the webpage when it's called. I at least get something now , thank you. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229824 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Thanks Redixx for the rapid response. I've tried your suggestion, just that it outputs everything including to top secret php code on the webpage when it's called. I at least get something now , thank you. Any suggestions? I assumed that's what you wanted to happen. You can't assign php code to a variable, unless you plan to use the exec() function or something (which is a generally ill-advised). Why not just echo the HTML and then include the page, instead of assigning it to a variable and then echoing it. What is it exactly you are trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229826 Share on other sites More sharing options...
cyberRobot Posted June 15, 2011 Share Posted June 15, 2011 What does file.php contain? If it contains something like a function, you should be able to include() or require() it at the beginning of your script. Then call the function where you want to display the file.php contents. Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229828 Share on other sites More sharing options...
cyberRobot Posted June 15, 2011 Share Posted June 15, 2011 For what it's worth, you should probably review the manual for include(): http://www.php.net/manual/en/function.include.php They provide a few examples of how it can be used. Also note that include isn't a function, so you don't need the parenthesis. require() is pretty much the same as include() except that it throws a fatal error if require() fails: http://www.php.net/manual/en/function.require.php Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229829 Share on other sites More sharing options...
jdrjsbl Posted June 15, 2011 Author Share Posted June 15, 2011 I guess this is what I'm trying to do: ******* form.php ******* some php code... <form> some php and html ... php form... </form> ----------------------- ****** config.php ****** <?php $foo='<table> <tr><td> // I would like to get the contents of form.php and add the to the rest of the contents of $foo. some text ' . file_get_contents("file.php") . ' some more text </tr></td> </table>'; ?> ----------------------- ****** file.php ****** <?php include("config.php"); ?> <html> <head> <?php // Some php code here. ?> <title>title</title> </head> <body> Please fill out our form.<br> <?php echo $foo; ?> <?php // Some php code here too. ?> </body> </html> cyberRobot: Thank you for your help. I've gone over it but nothing in it seems to help. Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229838 Share on other sites More sharing options...
trq Posted June 15, 2011 Share Posted June 15, 2011 There is no native way to do this in php. Easy enough to roll your own though. function inc($file) { ob_start(); include $file; return ob_get_contents(); } Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229847 Share on other sites More sharing options...
cyberRobot Posted June 15, 2011 Share Posted June 15, 2011 @jdrjsbl - First, let's ignore form.php. If you remove that include(), does file.php echo the $foo variable as expected? ****** config.php ****** <?php $foo='<table> <tr><td> // I would like to get the contents of form.php and add the to the rest of the contents of $foo. some text -- form.php goes here eventually -- some more text </tr></td> </table>'; ?> ****** file.php ****** <?php include("config.php"); ?> <html> <head> <?php // Some php code here. ?> <title>title</title> </head> <body> Please fill out our form.<br> <?php echo $foo; ?> <?php // Some php code here too. ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229856 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 I think you should look at a few lightweight CMS's and learn how their flow of code works, and then do something similar. Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229857 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 15, 2011 Share Posted June 15, 2011 ******* form.php ******* <?php $form=' some php code... <form> some php and html ... php form... </form>'; ?> ****** config.php ****** <?php include 'form.php'; $foo='<table> <tr><td> // I would like to get the contents of form.php and add the to the rest of the contents of $foo. some text ' .$form. ' some more text </tr></td> </table>'; ?> ****** file.php ****** <?php include("config.php"); ?> <html> <head> <?php // Some php code here. ?> <title>title</title> </head> <body> Please fill out our form.<br> <?php echo $foo; // Some php code here too. ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/239384-can-i-add-include-to-a-variable/#findComment-1229890 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.