Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3816

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>include</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.require.html">require</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.require-once.html">require_once</a></div>
 <div class="up"><a href="language.control-structures.html">Control Structures</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.include" class="sect1">
 <h2 class="title">include</h2>
 <p class="verinfo">(PHP 4, PHP 5)</p>
 <p class="simpara">
  The <em>include</em> statement includes and evaluates
  the specified file.
 </p>
 <p class="simpara">
  The documentation below also applies to  <span class="function"><a href="function.require.html" class="function">require</a></span>.
 </p>
 <p class="simpara">
  Files are included based on the file path given or, if none is given, the
  <a href="ini.core.html#ini.include-path" class="link">include_path</a> specified. If the file
  isn&#039;t found in the <a href="ini.core.html#ini.include-path" class="link">include_path</a>,
  <em>include</em> will finally check in the calling script&#039;s own
  directory and the current working directory before failing. The
  <em>include</em> construct will emit a
  <a href="" class="link">warning</a> if
  it cannot find a file; this is different behavior from
   <span class="function"><a href="function.require.html" class="function">require</a></span>, which will emit a
  <a href="" class="link">fatal error</a>.
 </p>
 <p class="simpara">
  If a path is defined — whether absolute (starting with a drive letter
  or <em>\</em> on Windows, or <em>/</em> on Unix/Linux
  systems) or relative to the current directory (starting with
  <em>.</em> or <em>..</em>) — the
  <a href="ini.core.html#ini.include-path" class="link">include_path</a> will be ignored
  altogether.  For example, if a filename begins with <em>../</em>,
  the parser will look in the parent directory to find the requested file.
 </p>
 <p class="simpara">
  For more information on how PHP handles including files and the include path,
  see the documentation for <a href="ini.core.html#ini.include-path" class="link">include_path</a>.
 </p>
 <p class="simpara">
  When a file is included, the code it contains inherits the
  <a href="language.variables.scope.html" class="link">variable scope</a> of the
  line on which the include occurs.  Any variables available at that line
  in the calling file will be available within the called file, from that
  point forward.
  However, all functions and classes defined in the included file have the
  global scope.
 </p>
 <p class="para">
  <div class="example" id="example-140">
   <p><strong>Example #1 Basic <em>include</em> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
vars.php<br /><span style="color: #0000BB">&lt;?php<br /><br />$color&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fruit&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span><br />test.php<br /><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;A<br /><br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green&nbsp;apple<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  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 <a href="language.constants.predefined.html" class="link">magic constants</a> which are
  evaluated by the parser before the include occurs.
 </p>
 <p class="para">
  <div class="example" id="example-141">
   <p><strong>Example #2 Including within functions</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;vars.php&nbsp;is&nbsp;in&nbsp;the&nbsp;scope&nbsp;of&nbsp;foo()&nbsp;so&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />*&nbsp;$fruit&nbsp;is&nbsp;NOT&nbsp;available&nbsp;outside&nbsp;of&nbsp;this&nbsp;&nbsp;*<br />*&nbsp;scope.&nbsp;&nbsp;$color&nbsp;is&nbsp;because&nbsp;we&nbsp;declared&nbsp;it&nbsp;*<br />*&nbsp;as&nbsp;global.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green&nbsp;apple<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  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
  <a href="language.basic-syntax.phpmode.html" class="link">valid PHP start
  and end tags</a>.
 </p>
 <p class="simpara">
  If &quot;<a href="filesystem.configuration.html#ini.allow-url-include" class="link">URL include wrappers</a>&quot;
  are enabled in PHP,
  you can specify the file to be included using a URL (via HTTP or
  other supported wrapper - see <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for a list
  of protocols) instead of a local pathname.  If the target server interprets
  the target file as PHP code, variables may be passed to the included
  file using a URL request string as used with HTTP GET.  This is
  not strictly speaking the same thing as including the file and having
  it inherit the parent file&#039;s variable scope; the script is actually
  being run on the remote server and the result is then being
  included into the local script.
 </p>
 <div class="warning"><strong class="warning">Warning</strong><p class="para">Windows versions of PHP
prior to PHP 4.3.0 do not support access of remote files via this function,
even if <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a> is enabled.
</p></div>
 <p class="para">
  <div class="example" id="example-142">
   <p><strong>Example #3 <em>include</em> through HTTP</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;This&nbsp;example&nbsp;assumes&nbsp;that&nbsp;www.example.com&nbsp;is&nbsp;configured&nbsp;to&nbsp;parse&nbsp;.php<br />*&nbsp;files&nbsp;and&nbsp;not&nbsp;.txt&nbsp;files.&nbsp;Also,&nbsp;'Works'&nbsp;here&nbsp;means&nbsp;that&nbsp;the&nbsp;variables<br />*&nbsp;$foo&nbsp;and&nbsp;$bar&nbsp;are&nbsp;available&nbsp;within&nbsp;the&nbsp;included&nbsp;file.&nbsp;*/<br /><br />//&nbsp;Won't&nbsp;work;&nbsp;file.txt&nbsp;wasn't&nbsp;handled&nbsp;by&nbsp;www.example.com&nbsp;as&nbsp;PHP<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'http://www.example.com/file.txt?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Won't&nbsp;work;&nbsp;looks&nbsp;for&nbsp;a&nbsp;file&nbsp;named&nbsp;'file.php?foo=1&amp;bar=2'&nbsp;on&nbsp;the<br />//&nbsp;local&nbsp;filesystem.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'http://www.example.com/file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />include&nbsp;</span><span style="color: #DD0000">'file.txt'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Works.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'file.php'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Works.<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <div class="warning"><strong class="warning">Warning</strong>
  <h1 class="title">Security warning</h1>
  <p class="para">
   Remote file may be processed at the remote server (depending on the file
   extension and the fact if the remote server runs PHP or not) but it still
   has to produce a valid PHP script because it will be processed at the
   local server. If the file from the remote server should be processed
   there and outputted only,  <span class="function"><a href="function.readfile.html" class="function">readfile()</a></span> is much better
   function to use. Otherwise, special care should be taken to secure the
   remote script to produce a valid and desired code.
  </p>
 </div>
 <p class="para">
  See also <a href="features.remote-files.html" class="link">Remote files</a>,
   <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> and  <span class="function"><a href="function.file.html" class="function">file()</a></span> for related
  information.
 </p>
 <p class="simpara">
  Handling Returns: <em>include</em> returns
  <em>FALSE</em> on failure and raises a warning. Successful
  includes, unless overridden by the included file, return
  <em>1</em>. It is possible to execute a  <span class="function"><a href="function.return.html" class="function">return</a></span>
  statement inside an included file in order to terminate processing in
  that file and return to the script which called it.  Also, it&#039;s possible
  to return values from included files. You can take the value of the
  include call as you would for a normal function.  This is not, however,
  possible when including remote files unless the output of the remote
  file has <a href="language.basic-syntax.phpmode.html" class="link">valid PHP start
  and end tags</a> (as with any local file).  You can declare the
  needed variables within those tags and they will be introduced at
  whichever point the file was included.
 </p>
 <p class="para">
  Because <em>include</em> is a special language construct,
  parentheses are not needed around its argument. Take care when comparing
  return value.
  <div class="example" id="example-143">
   <p><strong>Example #4 Comparing return value of include</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;won't&nbsp;work,&nbsp;evaluated&nbsp;as&nbsp;include(('vars.php')&nbsp;==&nbsp;'OK'),&nbsp;i.e.&nbsp;include('')<br /></span><span style="color: #007700">if&nbsp;(include(</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;works<br /></span><span style="color: #007700">if&nbsp;((include&nbsp;</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <div class="example" id="example-144">
   <p><strong>Example #5 <em>include</em> and the  <span class="function"><a href="function.return.html" class="function">return</a></span> statement</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
return.php<br /><span style="color: #0000BB">&lt;?php<br /><br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">;<br /><br />return&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span><br />noreturn.php<br /><span style="color: #0000BB">&lt;?php<br /><br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span><br />testreturns.php<br /><span style="color: #0000BB">&lt;?php<br /><br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;include&nbsp;</span><span style="color: #DD0000">'return.php'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;'PHP'<br /><br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;include&nbsp;</span><span style="color: #DD0000">'noreturn.php'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;1<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  <em>$bar</em> is the value <em>1</em> because the include
  was successful.  Notice the difference between the above examples.  The first uses
   <span class="function"><a href="function.return.html" class="function">return</a></span> within the included file while the other does not.
  If the file can&#039;t be included, <strong><code>FALSE</code></strong> is returned and
  <strong><code>E_WARNING</code></strong> is issued.
 </p>
 <p class="para">
  If there are functions defined in the included file, they can be used in the
  main file independent if they are before  <span class="function"><a href="function.return.html" class="function">return</a></span> or after.
  If the file is included twice, PHP 5 issues fatal error because functions
  were already declared, while PHP 4 doesn&#039;t complain about functions
  defined after  <span class="function"><a href="function.return.html" class="function">return</a></span>.
  It is recommended to use  <span class="function"><a href="function.include-once.html" class="function">include_once</a></span> instead of
  checking if the file was already included and conditionally return inside
  the included file.
 </p>
 <p class="simpara">
  Another way to &quot;include&quot; a PHP file into a variable is to capture the
  output by using the <a href="ref.outcontrol.html" class="link">Output Control
  Functions</a> with <em>include</em>. For example:
 </p>
 <p class="para">
  <div class="example" id="example-145">
   <p><strong>Example #6 Using output buffering to include a PHP file into a string</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_include_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.php'</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">get_include_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ob_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">ob_get_clean</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  In order to automatically include files within scripts, see also the
  <a href="ini.core.html#ini.auto-prepend-file" class="link">auto_prepend_file</a> and
  <a href="ini.core.html#ini.auto-append-file" class="link">auto_append_file</a>
  configuration options in <var class="filename">php.ini</var>.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Because this is a
language construct and not a function, it cannot be called using
<a href="functions.variable-functions.html" class="link">variable functions</a>.</span>
</p></blockquote>

 <p class="simpara">
  See also  <span class="function"><a href="function.require.html" class="function">require</a></span>,  <span class="function"><a href="function.require-once.html" class="function">require_once</a></span>,
   <span class="function"><a href="function.include-once.html" class="function">include_once</a></span>,  <span class="function"><a href="function.get-included-files.html" class="function">get_included_files()</a></span>,
   <span class="function"><a href="function.readfile.html" class="function">readfile()</a></span>,  <span class="function"><a href="function.virtual.html" class="function">virtual()</a></span>, and
  <a href="ini.core.html#ini.include-path" class="link">include_path</a>.
 </p>
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.require.html">require</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.require-once.html">require_once</a></div>
 <div class="up"><a href="language.control-structures.html">Control Structures</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>