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.
Question about include
Moderator: General Moderators
-
cpetercarter
- Forum Contributor
- Posts: 474
- Joined: Sat Jul 25, 2009 2:00 am
Re: Question about include
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:
and
Of course you can do things like:
Code: Select all
$requested_page = $_GET['page'];
include "page_".$requested_page.".php";
Code: Select all
if ($weather == 'rain') {
include 'umbrella.php';
}