Question about include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steve9876
Forum Newbie
Posts: 13
Joined: Tue Jul 21, 2009 7:34 pm

Question about include

Post by steve9876 »

Does include effectively put ?>...<?php tags before and after?

I was trying to include a file containing only a static array definition (let's say, the names of the months), but the file contents were being displayed when the script was run and not incorporated in the script. After a fair amount of trial-and-error, I tried enclosing the file contents in <?php...?> tags, and that did the trick.

This would make some sense: as I understand it, include is often (most often?) used to insert html boilerplate, but I don't believe I've seen this made clear.

While I'm on the subject, there are a couple of other things I'd like to clarify about includes.

Does it matter what the extension of the included file is?

As I understand it, includes are parsed and expanded statically, at load time. So you can't do something like "$filename=$basename . '01'; include $filename;" or " if A include fileA;". Do I have that right?

Thanks for your help.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Question about include

Post by cpetercarter »

If an include file contains php code, then you have to enclose the code in <?php ?> tags, otherwise the server will think you want to output the contents to the web-page.

Of course you can do things like:

Code: Select all

$requested_page = $_GET['page'];
include "page_".$requested_page.".php";
 
and

Code: Select all

if ($weather == 'rain') {
    include 'umbrella.php';
}
 
Post Reply