简体   繁体   English

在PHP中,include()如何正常工作?

[英]In PHP, how does include() exactly work?

How does include('./code.php'); include('./code.php'); work? 工作? I understand it is the equivalent of having the code "pasted" directly where the include occurs, but, for example: 我理解这相当于直接在包含发生的地方“粘贴”代码,但是,例如:

If I have two pages, page1.php and page2.php , of which I would need to manipulate different variables and functions in ./code.php , does include() create a copy of ./code.php , or does it essentially link back to the actual page ./code.php ? 如果我有两个页面, page1.phppage2.php ,我需要在./code.php操作不同的变量和函数, include()创建了./code.php的副本,或者它本质上是链接回实际页面./code.php

See the manual . 请参阅手册

If you include a text file, it will show as text in the document. 如果include文本文件,它将在文档中显示为文本。

include does not behave like copy-paste: include 表现得像复制粘贴:

test.php test.php的

<?php
echo '**';
$test = 'test';
include 'test.txt';
echo '**';
?>

test.txt 的test.txt

echo $test;

The example above will output: 上面的例子将输出:

**echo $test;**

If you are going to include PHP files, you still need to have the PHP tags <?php and ?> . 如果要包含PHP文件,仍需要PHP标记<?php?>

Also, normally parentheses are not put after include , like this: 此外,通常括号不includeinclude之后,如下所示:

include 'test.php';

Basically, when the interpreter hits an include 'foo.php'; 基本上,当翻译人员点击include 'foo.php'; statement, it opens the specified file, reads all its content, replaces the "include" bit with the code from the other file and continues with interpreting: 语句,它打开指定的文件,读取其所有内容,用另一个文件中的代码替换“include”位,并继续解释:

<?php
echo "hello";

include('foo.php');

echo "world";

becomes (theoretical) 成为(理论)

<?php
echo "hello";

?>
{CONTENT OF foo.php}
<?php

echo "world";

However, all this happens just in the memory, not on the disk. 但是,所有这些只发生在内存中,而不是发生在磁盘上。 No files are changed or anything. 没有文件被更改或任何东西。

It depends on how you include the code.php file. 这取决于您如何包含code.php文件。 If you include the code.php file into page1.php and then page2.php then you will have access to it's vars and it would effecitvely just "copy and paste" (beaware that is just used to make it easier to understand since the actual dynamics of that are explained above) the file in however if you link like: 如果你将code.php文件包含在page1.php中,然后将page2.php包含在内,那么你就可以访问它的vars,它会有效地“复制和粘贴”(beaware只是用来让它更容易理解,因为实际上面解释了动态的文件,但如果你链接像:

[code.php]
include('page1.php');
include('page2.php');

Then code.php will have access to all of the variables within page1.php but: 然后code.php可以访问page1.php中的所有变量,但是:

  • page1.php will not have access to vars in code.php page1.php将无法访问code.php中的vars
  • page2.php will not have access to vars in code.php page2.php将无法访问code.php中的vars

So in order to inherit the funcitonality of code.php inot both page1.php and page2.php be sure to do something like: 因此,为了inherit code.php的功能,请同时page1.phppage2.php

[page1.php]
include('code.php');

[page2.php]
include('code.php');

Then it will work as you expect. 然后它会像你期望的那样工作。 So just something to remember there. 所以只需记住一些东西。

Include does not behave like copy paste. 包含不像复制粘贴。

Here is a demonstration using PHP Strict Type Declarations 这是一个使用PHP严格类型声明的演示

<?php

// function.php (file)

declare (strict_types = 1);

function sum(int $a, int $b)
{
  return $a + $b;
}

/**
 * This will throw TypeError: 
 */

// echo sum(5.2, 5);

However, if I call this function from an external file 但是,如果我从外部文件调用此函数

<?php

// caller.php (file)

require_once 'function.php';

/**
 * This will Work instead of throwing a TypeError:
 */

echo sum(5.2, 5);

Strict types are enabled for the file that the declare statement was added (function.php) 为添加了声明语句的文件启用了严格类型(function.php)

It does not apply for any function calls made from an external file (caller.php) 它不适用于从外部文件(caller.php)进行的任何函数调用
because PHP will always defer to the caller when looking to evaluate strict types. 因为在查看严格类型时,PHP总是会调用调用者。

Require doesn't work like copy/paste 要求不像复制/粘贴那样工作

For further understanding, 为了进一步了解,
Short answer on how PHP script is Executed & 关于如何执行PHP脚本的简短回答
A more comprehensible explanation on what include/require really does in php 关于包含/要求在php中真正做什么的更容易理解的解释

include('./code.php'); // The Same As Pasting The Code From Code.PHP.

在任何情况下它都不会重定向到Code.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM