开发者

PHP: include\require behavior

whenever we include\require some file using php like

require ('links.php'); OR include ('links.php');

which one of the following two scenarios happen

    EXAMPLE

lets say there is file file.php with the follwing code

<?php
echo "my things";
?>
<br />

and we include this in

------
------
<?php
echo "about to include file";
include ('file.php')
?>
------
------

SCENARIO 1 : the included file's code is inserted in the parents\container files PHP Code and then the complete code is processed & HTML\result is generated....

meaning, first this code be first put like

------
------
<?php
echo "about to include file";
<?php
echo "my things";
?>
<br />
?>
------
------

then processed

开发者_开发百科SCENARIO 2 : the included file is first processed and the result is plugged in

meaning first the include file will be processed and the result will be obtained

mythings<br/>

and after that it is placed inside the parent\container\includer code and then that code will be processed neaning

------
------
<?php
echo "about to include file";
my things<br />
?>
------
------

and now it will be processed


Well, this might not be easiest to understand with just the "include" name...

So - what happens when you do

<?php
echo "including now...";
include "myFile.php";
echo "blah";
?>

then it will basically turn out like this:

<?php
echo "including now...";

?>
CONTENTS OF myFile.php HERE
<?php

echo "blah";
?>

Meaning that in your example it would look like this:

<?php
echo "about to include file";
?>
<?php
echo "my things";
?>
<br />
<?php
?>


It is scenario one.

Also note that require will only put in the code once! so:

<?php
echo "about to include file";
require ('file.php');
require ('file.php');
require ('file.php');
echo "included the file";
?>

will yield:

<?php
echo "about to include file";
?><?php
echo "my things";
?>
<br /><?
echo "included the file";
?>

whereas:

<?php
echo "about to include file";
include ('file.php');
include ('file.php');
include ('file.php');
echo "included the file";
?>

will yield:

<?php
echo "about to include file";
?><?php
echo "my things";
?>
<br /><?php
echo "my things";
?>
<br /><?php
echo "my things";
?>
<br /><?php
echo "included the file";
?>


It's scenario one. include is a simple mechanism to "inject" code at that line of code.

As a historical tidbit, before PHP 4.1, includes used to be processed even if the statement was in a block or condition that was never executed. Other than that, PHP does not have anything that would come close to your scenario 2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜