views:

108

answers:

2

I'm working on changing over a large, somewhat poorly written (but not terribly so), PHP website to use a class which stores functions which do things like write the standard page header with arguments to change things like the text of the header. The old version used a copied and pasted HTML header in each file with something like:

<div class="left"><?php include("left.php"); ?></div>

Where the included file has both non-echoed HTML and normal PHP. These includes seem ok to keep using, but I'm just not sure how they'll work, as to my knowledge, included files just replace the include() method directly.

Would it be better to:
-Re-write the included files to echo all standard HTML
-Simply move the content of these files into my new function where appropriate (to my knowledge, they're not used anywhere except where they would be in my new function)
-Something else

Thanks.

A: 

Writing your PHP to echo all the HTML is more than a little inefficient if you aren't actually parsing anything such as variables. That said, I can't seem to shake the habit of doing it myself. :)

A well designed page will use PHP only where necessary and I think your current method of including is probably the best method for your site.

If the majority of your site suddenly becomes dynamically generated it might be worth revisiting this question.

Andy Moore
+4  A: 

Read http://php.net/include:

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

PHP doesn't just copy and paste the code from the included file to replace the include statement, like C/C++ does. So you don't have to worry about the <?php and ?> tags in the included file messing up the file that is including it.

yjerem