vampke Posted March 6, 2007 Share Posted March 6, 2007 Hi guys, another beginner question I'm afraid.... If I enter the code below, as a result I will get only the txt-file includes, but not the raw html input. <? $header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> <html> <head> </head> <body>"; $header .= include ('incl/top.txt'); $header .= "</tr> <tr>"; $header .= include ('incl/menu.txt'); ?> Why won't this work? Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/ Share on other sites More sharing options...
obsidian Posted March 6, 2007 Share Posted March 6, 2007 First of all, the include() function does not return a string. It actually treats the included file as part of the current one. So, if I'm understanding what you're after, you want to print out your header and then include the text files. So, if you're looking to actually print out the straight text of those files, you need to use file_get_contents() or another file reading method that does return them as a string: <?php $header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"; $header .= "<html>\n"; $header .= "<head>\n"; $header .= "</head>\n"; $header .= "<body>\n"; $header .= file_get_contents('incl/top.txt'); $header .= "</tr>\n"; $header .= "<tr>\n"; $header .= file_get_contents('incl/menu.txt'); ?> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200762 Share on other sites More sharing options...
vampke Posted March 6, 2007 Author Share Posted March 6, 2007 thanks for quick reply. unfortunately no luck! now i don't get a return at all. maybe i should add that the whole file is actually included in another php file which is called. My first try returned the text in the includes, but not the html header text or the </tr><tr> between the 2 includes. The solution you propose doesn't return anything Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200767 Share on other sites More sharing options...
obsidian Posted March 6, 2007 Share Posted March 6, 2007 That's because in my sample, nothing is being done with it. You need to print out the variable. Simply add an "echo $header;" line to the end of the code above, and you should see it all presented. Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200776 Share on other sites More sharing options...
vampke Posted March 6, 2007 Author Share Posted March 6, 2007 great stuff! thank you it works don't quite understand the difference between the include and the file_get yet though Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200778 Share on other sites More sharing options...
obsidian Posted March 6, 2007 Share Posted March 6, 2007 don't quite understand the difference between the include and the file_get yet though Think of it this way (it wouldn't hurt to actually try this out): include() makes the included file part of the current file while file_get_contents() returns the contents of the file as a string. This in mind, if you include() a PHP file, the included file will actually be parsed and run just as if the code it contains were part of the parent file. However, if you run file_get_contents() on a PHP file and echo the contents, the actual code of the file would be printed to the screen. Try this out, and it may help with the difference: File 1 (file01.php): <?php $greeting = "Hello, how are you"; echo $greeting; ?> File 2 - Include example (file02.php): <?php header("Content-type: text/plain"); include("file01.php"); ?> File 3 - Get Contents example (file03.php): <?php header("Content-type: text/plain"); $file = file_get_contents('file01.php'); echo $file; ?> Notice that in the include() example, the included file is actually run as part of the script, but in the file_get_contents() example, you are pulling the contents of the file as a string. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200800 Share on other sites More sharing options...
vampke Posted March 6, 2007 Author Share Posted March 6, 2007 Hope this helps. very much so, thank you! Link to comment https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/#findComment-200809 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.