1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
<HTML
><HEAD
><TITLE
>include</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.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
NAME="HTTP_EQUIV"
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
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"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Control Structures</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.require-once.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="function.include"
><A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
></A
></H1
><P
> The <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> statement includes and evaluates
the specified file.
</P
><P
> If "URL fopen wrappers" are enabled in PHP (which they are in the
default configuration), you can specify the file to be
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed using an URL instead of a local
pathname. See <A
HREF="features.remote-files.html"
>Remote
files</A
> and <A
HREF="function.fopen.html"
><B
CLASS="function"
>fopen()</B
></A
> for more information.
</P
><P
> An important note about how this works is that when a file is
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed or <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>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 <A
HREF="language.basic-syntax.html#language.basic-syntax.phpmode"
>valid PHP start and end
tags</A
>.
</P
><P
> This happens each time the <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> statement
is encountered, so you can use an <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>
statement within a looping structure to include a number of
different files.
<DIV
CLASS="informalexample"
><A
NAME="AEN3649"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $files = array ('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
include $files[$i];
}
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
</P
><P
> <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> differs from
<A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
> in that the include statement is
re-evaluated each time it is encountered (and only when it is
being executed), whereas the <A
HREF="function.require.html"
><B
CLASS="function"
>require()</B
></A
>
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 <A
HREF="control-structures.html#control-structures.if"
>if</A
> statement whose
condition evaluated to false).
</P
><P
> Because <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> is a special language
construct, you must enclose it within a statement block if it is
inside a conditional block.
<DIV
CLASS="informalexample"
><A
NAME="AEN3658"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> /* 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);
}
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
</P
><P
> In both PHP 3 and PHP 4, it is possible to execute a
<TT
CLASS="literal"
>return</TT
> statement inside an
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>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 PHP 3, the <TT
CLASS="literal"
>return</TT
> may not
appear inside a block unless it's a function block, in which case
the <TT
CLASS="literal"
>return</TT
> applies to that function and not the
whole file. In PHP 4, however, this restriction does not
exist. Also, PHP 4 allows you to return values from
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed files. You can take the value of
the <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> call as you would a normal
function. This generates a parse error in PHP 3.
</P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3667"
></A
><P
><B
>Example 11-1. <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> in PHP 3 and PHP 4</B
></P
><P
> Assume the existence of the following file (named
<TT
CLASS="filename"
>test.inc</TT
>) in the same directory as the main
file:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
echo "Before the return <br>\n";
if (1) {
return 27;
}
echo "After the return <br>\n";
?>
</PRE
></TD
></TR
></TABLE
>
</P
><P
> Assume that the main file (<TT
CLASS="filename"
>main.html</TT
>)
contains the following:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
$retval = include ('test.inc');
echo "File returned: '$retval'<br>\n";
?>
</PRE
></TD
></TR
></TABLE
>
</P
><P
> When <TT
CLASS="filename"
>main.html</TT
> is called in PHP 3, it will
generate a parse error on line 2; you can't take the value of an
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> in PHP 3. In PHP 4, however, the
result will be:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="screen"
> Before the return
File returned: '27'
</PRE
></TD
></TR
></TABLE
>
</P
><P
> Now, assume that <TT
CLASS="filename"
>main.html</TT
> has been altered
to contain the following:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
include ('test.inc');
echo "Back in main.html<br>\n";
?>
</PRE
></TD
></TR
></TABLE
>
</P
><P
> In PHP 4, the output will be:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="screen"
> Before the return
Back in main.html
</PRE
></TD
></TR
></TABLE
>
However, PHP 3 will give the following output:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="screen"
> Before the return
27Back in main.html
Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5
</PRE
></TD
></TR
></TABLE
>
</P
><P
> The above parse error is a result of the fact that the
<TT
CLASS="literal"
>return</TT
> statement is enclosed in a non-function
block within <TT
CLASS="filename"
>test.inc</TT
>. When the return is
moved outside of the block, the output is:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="screen"
> Before the return
27Back in main.html
</PRE
></TD
></TR
></TABLE
>
</P
><P
> The spurious '27' is due to the fact that PHP 3 does not support
<TT
CLASS="literal"
>return</TT
>ing values from files like that.
</P
></DIV
></TD
></TR
></TABLE
><P
> When a file is <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed, the code it
contains inherits the variable scope of the line on which the
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> occurs. Any variables available at
that line in the calling file will be available within the called
file. If the <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
> 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.
</P
><P
> If the <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed file is called via HTTP
using the fopen wrappers, and if the target server interprets the
target file as PHP code, variables may be passed to the
<A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ed file using an URL request string as
used with HTTP GET. This is not strictly speaking the same thing
as <A
HREF="function.include.html"
><B
CLASS="function"
>include()</B
></A
>ing 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.
<DIV
CLASS="informalexample"
><A
NAME="AEN3700"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> /* This example assumes that someserver is configured to parse .php
* files and not .txt files. Also, 'works' here means that the variables
* $varone and $vartwo are available within the include()ed file. */
/* Won't work; file.txt wasn't handled by someserver. */
include ("http://someserver/file.txt?varone=1&vartwo=2");
/* Won't work; looks for a file named 'file.php?varone=1&vartwo=2'
* on the local filesystem. */
include ("file.php?varone=1&vartwo=2");
/* Works. */
include ("http://someserver/file.php?varone=1&vartwo=2");
$varone = 1;
$vartwo = 2;
include ("file.txt"); /* Works. */
include ("file.php"); /* Works. */
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
</P
><P
> 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
>,
and <A
HREF="function.virtual.html"
><B
CLASS="function"
>virtual()</B
></A
>.
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.require.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.require-once.html"
>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"
>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
>
|