M2769 Posted August 26, 2007 Share Posted August 26, 2007 I have a question about the include('') tag; I want to include another document, say "page2.html" as part of my original page, "page1.php". Both documents have opening and closing "html" and "body" tags, so my question is could opening a second html and body tag inside of the first cause problems? page2.html has it's own <html> tag, so it would look something like... <html> code in "page1.php"... <html> (where "page2.html" is included) code in "page2.html"... </html> (end of "page2.html") some more code in "page1.php" </html> So I guess the simple questions is...would nesting <html> or <body> tags be a problem using the include('') or should I omit these tags from the document being included? Thanks, Marcus Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/ Share on other sites More sharing options...
trq Posted August 26, 2007 Share Posted August 26, 2007 Firstly. PHP has functions, not tags. include is actually a statement, hance it does not need the () around its arguments. Anyway.... So I guess the simple questions is...would nesting <html> or <body> tags be a problem using the include('') or should I omit these tags from the document being included? Yes it will be a problem. It will make your outputted html invalid and most likely it will not display properly. Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/#findComment-334567 Share on other sites More sharing options...
M2769 Posted August 26, 2007 Author Share Posted August 26, 2007 Thanks for the quick reply. I've been doing HTML for a long time and I'm just starting to get into PHP, so some of the old terms have stuck with me, my mistake. I didn't know the include statement did not need parentheses though, that's just the way I have seen it in books, but it's good to know . I tried doing what I previously described and it worked in that one example, but I see what you mean. It's probably better to avoid it to prevent any future problems. If I omit anything that would not normally be part of the body from in my included document, there shouldn't be any conflict through right? The document won't display properly on it's own, but that's not my intention; I just want it to display properly as part of the first document. Thanks again . Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/#findComment-334574 Share on other sites More sharing options...
schme16 Posted August 26, 2007 Share Posted August 26, 2007 if you'd like the documents that are being included top be valid html when viewed directly you could always leave all the tags as they are (i.e. the <body> and <html> tags) and use and explode function to take the unnecessary tags out, like so: <?php $file_path = 'path_to_your_file'; $file_contents = file_get_contents($file_path); $file_contents = explode('<body>', $file_contents); $file_contents = explode('</body>', $file_contents[1]); print $file_contents[0]; ?> Granted its a fair bit longer than simply using include, but it will mean that the pages will be still valid, and you can test them properly when viewing them directly... probably of no help, but hey....its out there for others now... Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/#findComment-334608 Share on other sites More sharing options...
M2769 Posted August 30, 2007 Author Share Posted August 30, 2007 It's always great to have alternatives. I've found some of my pages that were not intended to be viewed separately or by themselves, have shown up as results on search engines, so while the file_get_contents function may be a bit more complicated than simply using include it is probably a more fail-safe method and worth it in the long run. The testing part you mentioned would be a big help too, since local PHP files don't display properly in a browser (for lack of a paser I believe) without installing additional programs or a web server or something like that. Thanks to both of your for your help . Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/#findComment-337540 Share on other sites More sharing options...
M2769 Posted August 30, 2007 Author Share Posted August 30, 2007 Disregard the last bit of my previous post, I was thinking one step ahead. The same is true about being able to test the documents though since they include tags that would otherwise be omitted, such as stylesheets. Quote Link to comment https://forums.phpfreaks.com/topic/66767-solved-include-tag-question/#findComment-337853 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.