Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 1182

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>include</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Control Structures"
HREF="control-structures.html"><LINK
REL="PREVIOUS"
TITLE="require"
HREF="function.require.html"><LINK
REL="NEXT"
TITLE="require_once"
HREF="function.require-once.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.require.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 12. Control Structures</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.require-once.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="function.include"
></A
><A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
></H1
><P
>&#13;    The <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> statement includes and evaluates
    the specified file.
   </P
><P
>&#13;     The documentation below also applies to <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>.
     The two constructs are identical in every way except how they handle
     failure.  <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> produces a 
     <A
HREF="phpdevel-errors.html#internal.e-warning"
>Warning</A
> while <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>
     results in a <A
HREF="phpdevel-errors.html#internal.e-error"
>Fatal Error</A
>.
     In other words, use <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
> if you want 
     a missing file to halt processing of the page.  <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> does 
     not behave this way, the script will continue regardless.  Be sure to have an 
     appropriate <A
HREF="configuration.directives.html#ini.include-path"
>include_path</A
> setting as well.
   </P
><P
>&#13;     When a file is included, the code it contains inherits the
     <A
HREF="language.variables.scope.html"
>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.
   </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5178"
></A
><P
><B
>Example 12-3. Basic <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>vars.php
&#60;?php

$color = 'green';
$fruit = 'apple';

?&#62;

test.php
&#60;?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;     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.
   </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5184"
></A
><P
><B
>Example 12-4. Including within functions</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

function foo()
{
global $color;

    include 'vars.php';

    echo "A $color $fruit";
}

/* vars.php is in the scope of foo() so     *
 * $fruit is NOT available outside of this  *
 * scope.  $color is because we declared it *
 * as global.                               */

foo();                    // A green apple
echo "A $color $fruit";   // A green

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;     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.html#language.basic-syntax.phpmode"
>valid PHP start
     and end tags</A
>.
   </P
><P
>&#13;     If "<A
HREF="ref.filesystem.html#ini.allow-url-fopen"
>URL fopen wrappers</A
>"
     are enabled in PHP (which they are in the default configuration),
     you can specify the file to be included using an URL (via HTTP or
     other supported wrapper - see <A
HREF="wrappers.html"
>Appendix I</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 an 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'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"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>Windows
versions of <TT
CLASS="literal"
>PHP</TT
> prior to PHP 4.3 do not
support accessing remote files via this function, even if
<A
HREF="ref.filesystem.html#ini.allow-url-fopen"
>allow_url_fopen</A
> is enabled.
</P
></TD
></TR
></TABLE
></DIV
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5197"
></A
><P
><B
>Example 12-5. <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> through HTTP</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

/* This example assumes that www.example.com is configured to parse .php *
 * files and not .txt files. Also, 'Works' here means that the variables *
 * $foo and $bar are available within the included file.                 */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&#38;bar=2';

// Won't work; looks for a file named 'file.php?foo=1&#38;bar=2' on the
// local filesystem.
include 'file.php?foo=1&#38;bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&#38;bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     See also <A
HREF="features.remote-files.html"
>Remote files</A
>,
     <A
HREF="function.fopen.html"
><B
CLASS="function"
>fopen()</B
></A
> and <A
HREF="function.file.html"
><B
CLASS="function"
>file()</B
></A
> for related 
     information.
   </P
><P
>&#13;     Because <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> and <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>
     are special language constructs, you must enclose them within a statement
     block if it's inside a conditional block.
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5208"
></A
><P
><B
>Example 12-6. include() and conditional blocks</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

// 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;
}

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    Handling Returns: It is possible to execute a <A
HREF="function.return.html"
><B
CLASS="function"
>return()</B
></A
> 
    statement inside an included file in order to terminate processing in that 
    file and return to the script which called it.  Also, it's possible to return 
    values from included files.  You can take the value of the include call as 
    you would a normal function.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
     In PHP 3, the return may not appear inside a block unless it's
     a function block, in which case the <A
HREF="function.return.html"
><B
CLASS="function"
>return()</B
></A
> applies 
     to that function and not the whole file.
    </P
></BLOCKQUOTE
></DIV
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5217"
></A
><P
><B
>Example 12-7. <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> and the <A
HREF="function.return.html"
><B
CLASS="function"
>return()</B
></A
> statement</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>return.php
&#60;?php

$var = 'PHP';

return $var;

?&#62;

noreturn.php
&#60;?php

$var = 'PHP';

?&#62;

testreturns.php
&#60;?php

$foo = include 'return.php';

echo $foo; // prints 'PHP'

$bar = include 'noreturn.php';

echo $bar; // prints 1

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    <TT
CLASS="literal"
>$bar</TT
> is the value <TT
CLASS="literal"
>1</TT
> because the include 
    was successful.  Notice the difference between the above examples.  The first uses 
    <A
HREF="function.return.html"
><B
CLASS="function"
>return()</B
></A
> within the included file while the other does not.  
    A few other ways to "include" files into variables are with 
    <A
HREF="function.fopen.html"
><B
CLASS="function"
>fopen()</B
></A
>, <A
HREF="function.file.html"
><B
CLASS="function"
>file()</B
></A
> or by using 
    <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> along with 
    <A
HREF="ref.outcontrol.html"
>Output Control Functions</A
>.
   </P
><P
>&#13;    See also <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>, <A
HREF="function.require-once.html"
><B
CLASS="function"
>require_once()</B
></A
>,
    <A
HREF="function.include-once.html"
><B
CLASS="function"
>include_once()</B
></A
>, <A
HREF="function.readfile.html"
><B
CLASS="function"
>readfile()</B
></A
>,
    <A
HREF="function.virtual.html"
><B
CLASS="function"
>virtual()</B
></A
>, and
    <A
HREF="configuration.directives.html#ini.include-path"
>include_path</A
>.
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.require.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.require-once.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="control-structures.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.require-once.html"
><B
CLASS="function"
>require_once()</B
></A
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>