<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.include.php',
    1 => 'include',
    2 => 'include',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'function.require.php',
    1 => 'require',
  ),
  'next' => 
  array (
    0 => 'function.require-once.php',
    1 => 'require_once',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/include.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.include" class="sect1">
 <h2 class="title">include</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="simpara">
  The <code class="literal">include</code> expression includes and evaluates
  the specified file.
 </p>
 <p class="simpara">
  The documentation below also applies to <span class="function"><a href="function.require.php" 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.php#ini.include-path" class="link">include_path</a> specified. If the file
  isn&#039;t found in the <a href="ini.core.php#ini.include-path" class="link">include_path</a>,
  <code class="literal">include</code> will finally check in the calling script&#039;s own
  directory and the current working directory before failing. The
  <code class="literal">include</code> construct will emit an
  <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if
  it cannot find a file; this is different behavior from
  <span class="function"><a href="function.require.php" class="function">require</a></span>, which will emit an
  <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong>.
 </p>
 <p class="simpara">
  Note that both <code class="literal">include</code> and <code class="literal">require</code>
  raise additional <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>s, if the file cannot be
  accessed, before raising the final <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> or
  <strong><code><a href="errorfunc.constants.php#constant.e-error">E_ERROR</a></code></strong>, respectively.
 </p>
 <p class="simpara">
  If a path is defined — whether absolute (starting with a drive letter
  or <code class="literal">\</code> on Windows, or <code class="literal">/</code> on Unix/Linux
  systems) or relative to the current directory (starting with
  <code class="literal">.</code> or <code class="literal">..</code>) — the
  <a href="ini.core.php#ini.include-path" class="link">include_path</a> will be ignored
  altogether.  For example, if a filename begins with <code class="literal">../</code>,
  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.php#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.php" 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-1">
   <p><strong>Приклад #1 Basic <code class="literal">include</code> 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 </span><span style="color: #007700">= </span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fruit </span><span style="color: #007700">= </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 </span><span style="color: #DD0000">"A </span><span style="color: #0000BB">$color</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// A<br /><br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"A </span><span style="color: #0000BB">$color</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// A green 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.magic.php" class="link">magic constants</a> which are
  evaluated by the parser before the include occurs.
 </p>
 <p class="para">
  <div class="example" id="example-2">
   <p><strong>Приклад #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 </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />    global </span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br /><br />    include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />    echo </span><span style="color: #DD0000">"A </span><span style="color: #0000BB">$color</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* vars.php is in the scope of foo() so     *<br />* $fruit is NOT available outside of this  *<br />* scope.  $color is because we declared it *<br />* as global.                               */<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();                    </span><span style="color: #FF8000">// A green apple<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A </span><span style="color: #0000BB">$color</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$fruit</span><span style="color: #DD0000">"</span><span style="color: #007700">;   </span><span style="color: #FF8000">// A 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.php" class="link">valid PHP start
  and end tags</a>.
 </p>
 <p class="simpara">
  If &quot;<a href="filesystem.configuration.php#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.php" class="xref">Підтримувані протоколи та обгортки</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>
 <p class="para">
  <div class="example" id="example-3">
   <p><strong>Приклад #3 <code class="literal">include</code> 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">/* This example assumes that www.example.com is configured to parse .php<br />* files and not .txt files. Also, 'Works' here means that the variables<br />* $foo and $bar are available within the included file. */<br /><br />// Won't work; file.txt wasn't handled by www.example.com as PHP<br /></span><span style="color: #007700">include </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">// Won't work; looks for a file named 'file.php?foo=1&amp;bar=2' on the<br />// local filesystem.<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works.<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'http://www.example.com/file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <div class="warning"><strong class="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.php" 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.php" class="link">Remote files</a>,
  <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> and <span class="function"><a href="function.file.php" class="function">file()</a></span> for related
  information.
 </p>
 <p class="simpara">
  Handling Returns: <code class="literal">include</code> returns
  <code class="literal">FALSE</code> on failure and raises a warning. Successful
  includes, unless overridden by the included file, return
  <code class="literal">1</code>. It is possible to execute a <span class="function"><a href="function.return.php" 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.php" 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 <code class="literal">include</code> is a special language construct,
  parentheses are not needed around its argument. Take care when comparing
  return value.
  <div class="example" id="example-4">
   <p><strong>Приклад #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">// won't work, evaluated as include(('vars.php') == TRUE), i.e. include('1')<br /></span><span style="color: #007700">if (include(</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">) == </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// works<br /></span><span style="color: #007700">if ((include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">) == </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">) {<br />    echo </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-5">
   <p><strong>Приклад #5 <code class="literal">include</code> and the <span class="function"><a href="function.return.php" 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 </span><span style="color: #007700">= </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">;<br /><br />return </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 </span><span style="color: #007700">= </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 </span><span style="color: #007700">= include </span><span style="color: #DD0000">'return.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 'PHP'<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= include </span><span style="color: #DD0000">'noreturn.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$bar</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 1<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  <code class="literal">$bar</code> is the value <code class="literal">1</code> because the include
  was successful.  Notice the difference between the above examples.  The first uses
  <span class="function"><a href="function.return.php" class="function">return</a></span> within the included file while the other does not.
  If the file can&#039;t be included, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned and
  <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></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.php" class="function">return</a></span> or after.
  If the file is included twice, PHP will raise a fatal error because the
  functions were already declared.
  It is recommended to use <span class="function"><a href="function.include-once.php" 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.php" class="link">Output Control
  Functions</a> with <code class="literal">include</code>. For example:
 </p>
 <p class="para">
  <div class="example" id="example-6">
   <p><strong>Приклад #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 </span><span style="color: #007700">= </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 </span><span style="color: #0000BB">get_include_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">is_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)) {<br />        </span><span style="color: #0000BB">ob_start</span><span style="color: #007700">();<br />        include </span><span style="color: #0000BB">$filename</span><span style="color: #007700">;<br />        return </span><span style="color: #0000BB">ob_get_clean</span><span style="color: #007700">();<br />    }<br />    return </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.php#ini.auto-prepend-file" class="link">auto_prepend_file</a> and
  <a href="ini.core.php#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">Зауваження</strong>: <span class="simpara">Оскільки це мовна
конструкція, а не функція, її не можна викликати через <a href="functions.variable-functions.php" class="link">змінні-функції</a> або <a href="functions.arguments.php#functions.named-arguments" class="link">названі параметри</a>.</span></p></blockquote>

 <p class="simpara">
  See also <span class="function"><a href="function.require.php" class="function">require</a></span>, <span class="function"><a href="function.require-once.php" class="function">require_once</a></span>,
  <span class="function"><a href="function.include-once.php" class="function">include_once</a></span>, <span class="function"><a href="function.get-included-files.php" class="function">get_included_files()</a></span>,
  <span class="function"><a href="function.readfile.php" class="function">readfile()</a></span>, <span class="function"><a href="function.virtual.php" class="function">virtual()</a></span>, and
  <a href="ini.core.php#ini.include-path" class="link">include_path</a>.
 </p>
</div><?php manual_footer($setup); ?>