★ wanayoo — archive 1999 http://www.php.net/manual/function.include.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Control Structures
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Control Structures
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do..while
* for
* foreach
* break
* continue
* switch
* require
* include
* require_once
* include_once
Manual: include
View the source code for this pageSearch the site



Previous page
 require
 Updated
Mon, 07 Aug 2000
require_once 
Next page


include()

The include() statement includes and evaluates the specified file.

An important note about how this works is that when a file is include()ed or require()ed, 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.

This happens each time the include() statement is encountered, so you can use an include() statement within a looping structure to include a number of different files.


$files = array ('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
    include $files[$i];
}
     

include() differs from require() in that the include statement is re-evaluated each time it is encountered (and only when it is being executed), whereas the require() statement is replaced by the required file when it is first encountered, whether the contents of the file will be evaluated or not (for example, if it is inside an if statement whose condition evaluated to false).

Because include() is a special language construct, you must enclose it within a statement block if it is inside a conditional block.


/* This is WRONG and will not work as desired. */
 
if ($condition)
    include($file);
else
    include($other);
 
/* This is CORRECT. */
 
if ($condition) {
    include($file);
} else {
    include($other);
}
     

In both PHP3 and PHP4, it is possible to execute a return statement inside an include()ed file, in order to terminate processing in that file and return to the script which called it. Some differences in the way this works exist, however. The first is that in PHP3, the return may not appear inside a block unless it's a function block, in which case the return applies to that function and not the whole file. In PHP4, however, this restriction does not exist. Also, PHP4 allows you to return values from include()ed files. You can take the value of the include() call as you would a normal function. This generates a parse error in PHP3.

Example 11-1. include() in PHP3 and PHP4

Assume the existence of the following file (named test.inc) in the same directory as the main file:

<?php
echo "Before the return <br>\n";
if (1) {
    return 27;
}
echo "After the return <br>\n";
?>
     

Assume that the main file (main.html) contains the following:

<?php
$retval = include ('test.inc');
echo "File returned: '$retval'<br>\n";
?>
     

When main.html is called in PHP3, it will generate a parse error on line 2; you can't take the value of an include() in PHP3. In PHP4, however, the result will be:

Before the return
File returned: '27'
     

Now, assume that main.html has been altered to contain the following:

<?php
include ('test.inc');
echo "Back in main.html<br>\n";
?>
     

In PHP4, the output will be:

Before the return
Back in main.html
     
However, PHP3 will give the following output:

Before the return 
27Back in main.html

Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5
     

The above parse error is a result of the fact that the return statement is enclosed in a non-function block within test.inc. When the return is moved outside of the block, the output is:

Before the return
27Back in main.html
     

The spurious '27' is due to the fact that PHP3 does not support returning values from files like that.

Please note that both include() and require() actually pull the contents of the target file into the calling script file itself; they do not call the target via HTTP or anything like that. So any variable set in the scope in which the inclusion happens will be available within the included file automatically, since it has effectively become a part of the calling file.


include ("file.inc?varone=1&vartwo=2"); /* Won't work. */

$varone = 1;
$vartwo = 2;
include ("file.inc");  /* $varone and $vartwo will be available in file.inc */
     

Don't be misled by the fact that you can require or include files via HTTP using the Remote files feature; the above holds true regardless.

See also readfile(), require(), and virtual().


User Contributed Notes: include


carl@againstdemons.freeserve.co.uk
27-Jan-1999 06:18
require() is faster than include() too, so if you can use it, do so!


garretg@otable.com
23-Feb-1999 11:04
The path for the include command is set by the include_path setting in the PHP3.INI file, which defaults to the current directory.

For a site-wide include that doesn't require settings in PHP3.INI, consider using something like:
<PRE>
require($DOCUMENT_ROOT . "/header.html");
</PRE>

For security reasons, though, be careful about including PHP code this way... it's better to keep it outside the document root directory, where it is only accessible by file sytem commands.



lars@dybdahl.dk
28-Mar-1999 01:29
if file a includes file b, and file b wants to include file c, then include() does not provide any way to include c with a path relative to b, it can only specificy paths relative to a. This is a major obstacle for creating larger projects directly in PHP3.


jja@cmsco.com
09-Apr-1999 01:46
Well... you could always abandon the use of relative paths in favor of explicit paths relative to $DOCUMENT_ROOT.


rmm@dridus.com
10-Jul-1999 08:15
Regarding the security issue noted above, if you want nobody to see the contents of your include files, and you use Apache, you can try stuffing:
<pre>
Order deny,allow
Deny from all
</pre>
into your .htaccess file for where your include file are stored (I use <i>projectdir</i>/include). This also assumes that your Apache setup is configured with <code>AllowOverride all</code> or similar.

This works by virtue of PHP not caring one way or the other about Apache's .htaccess.



mainframe@thewatercooler.com
29-Jul-1999 02:23
If you're using include(), you will probably want to verify the existence of the included file with a file_exists() first, so that if by mistake the file doesn't exist, evil users won't see your directory structures.

I use this procedure in a set of php3's that provide the authentication for my site, where you call the php3 with a given filename, that is displayed only if the user has been authenticated first.



fran0382@tc.umn.edu
24-Aug-1999 02:37
You can use fopen() to include files from a web or ftp site like this:
fopen("http://www.somesite.edu/foo.html", "r"); If you do that though, make sure you include error checking to see if the file was actually opened or not.




08-Sep-1999 10:37
you can do this
<pre>
&lt;?
$scriptname = "dillwack.php3";
$args = "?id=2332&hits=233";
$path = $scriptname.$args;
include($path); /* or */ require($path); ?></pre>

It worked on <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fwww.dtheatre.com%26quot%3B%26gt%3Bdtheatre.com%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>


mhobson@pacbell.net
08-Nov-1999 03:46
Re: Passing arguments to included scripts--
If you are using Apache mod_php, try "virtual($url-with-parameters)";
You can make the url be a script on your site or one
on some other site.
Docs for this are in the Apache section of the function reference.



mhobson@pacbell.net
08-Nov-1999 03:57
<B>include</B> loads and runs a PHP3 Script
<B>But</B> the included script will <B>not</B>
receive <I>any</I> CGI variables unless you
make the <I>web server</I> load and run it by
calling <B>include</B> with a URL instead of
the local file name.

Instead of:

<PRE>
include("myscript.inc?arg=val");
</PRE>
use:

<PRE>
include("http://my.server.com/cgi-bin/myscript.php3?arg=val");
</PRE>
or whatever URL would make that work on your server. This is an
alternate solution for those who aren't using Apache mod_php3.





pdt@pdt.cx
27-Nov-1999 02:38
I ran across multiple inclusion problems with a recent project.. I had one file global.php3 that was included in several places so I added a $GLOBALINCLUDED variable in the file and anywhere I included the global file i did this:

<PRE>if(!isset($GLOBALINCLUDED) {
include "global.php3";
}



vksgeneric@hotmail.com
08-Dec-1999 01:27
probably better than the suggestion above is a thing similar to the #ifndef...#define...#endif in the c/c++ header files. Put this at the beginning of the file included:

if(isset($GLOBAL_INC)){

return;

}

$GLOBAL_INC = "set";


and remember to use include() rather than require() to include that file.



digita1l@hotmail.com
25-Dec-1999 12:32
a major security risk is leaving *.inc files world readable. In apache you can add the following to your httpd.conf file to deny access to *.inc:

<Files ~ ".inc">
Order allow,deny
Deny from all
</Files>



nate@nojunk_allmed.net
13-Jan-2000 07:01
I also ran into the problem that many others reported about multiple includes.
I liked the idea the person had about useing something simular to c's #ifdef and #def, but when I tried his example it didn't work becouse I was including the file inside of a function the first time, which made the varible local. Then when I tried to include it again it didn't find the previous local varible.

The solution I came up with was to use constants like so:

if(GETSETTINGS_INCLUDED==1){
return;
}
define("GETSETTINGS_INCLUDED", 1);

Since constants are always global, this works great. Of course the fact that constants are global might not be such a great idea, I can see where it would be handy to have private constants in a class definition. But this will work.



btol45@calvin.edu
12-Feb-2000 04:07
One of the problems I've struggled with for a long time was sending a header *after* I included a file:
<PRE>
include("somthing.php");
header("Location: something_else.php");
</PRE>

This never worked for me, but it does work if you
wrap the include in an if statement:
<PRE>
if (include("something.php")) {}
header("Location: something_else.php");
</PRE>
Now you can include the file AND redirect.



milan@insource.nl
25-Feb-2000 11:07
I ran into a security issue where web users were able to just download the .inc files and while doing that, their sourcecode with ofcourse passwords... My solution for this problem (without having to create .htaccess files) is adding the .inc extension to "AddType application/x-httpd-php3 .php3" in the appache httpd.conf file. Or if you can't edit the httpd.conf: just give your include files the same extension as your normal php files.


jengo@NOSPAMmail.com
17-May-2000 11:05
Just a quick note on something that drove me nuts for about an hour last night. I was tring to included a common footer into my program and its was completly refussing to add the file in there. It keep on saying it doesn't exists.

Finally after an hour of 4 letter words I figured out that you need to chmod the included files to 755.

This way someone else does go through the hell I did :)



simon.li@hongkong.com
02-Jul-2000 07:18
It sound a little stupid. But it is another remind.

If you are using a maching which run windows + PHP Win32, and finally going to put all your work (htm, php3, etc) to a unix server, then you must be carefully not to mistype your filenames.

Coz in windows file.php and FILE.php are the same thing, but not in unix. If you mistyped, then some of your code works and some doesn't and it will drive you crazy.



f97-ali@f.kth.se
24-Jul-2000 04:19
php3 will happily include the same file multiple times, which is usually a bad idea. This is why you should add C-style inclusion-guards in the beginning of inc-files.


If you have an inc-file called login.inc, you should do something like this in the beginning:

<PRE>
if defined( "LOGIN_INC" )
return;
define( "LOGIN_INC", TRUE );

#code goes here...
...
</PRE>
This way your inc will only be included once.



breuerm@execpc.com
24-Jul-2000 05:00
Because require statements are replaced with the contents of the included file, the following construct does not work:


if (defined("MYSOURCE_PHP")) { return; } define("MYSOURCE_PHP",1);


If the file is being required for the second time, the calling script returns at this point, and no further code is executed. To get around this, I used:


if (!defined("MYSOURCE_PHP)) { define("MYSOURCE_PHP",1); /* function definitions */ }


This seems to resolve the issue for me using 4.0.1pl2.



oystein@edge.no
11-Aug-2000 08:05
To prevent users from being able to view your include files with passwords and/ or other sensitive data, you should always add a .php(3) extension, e.g.:

include("header.inc.php");
include("config.inc.php");

This way you won't need to specifically hide your include files with, for instance, Apache directives.



 About Notes


Previous page
 require
 Updated
Mon, 07 Aug 2000
require_once 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.