Jump to content

[SOLVED] include('') tag question


M2769

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :).

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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 :).

Link to comment
Share on other sites

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.

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.