在PHP中,include()到底如何工作? [英] In PHP, how does include() exactly work?
问题描述
include('./code.php');
如何工作?我了解这等同于将代码直接粘贴"到包含发生的位置,但是,例如:
How does include('./code.php');
work? I understand it is the equivalent of having the code "pasted" directly where the include occurs, but, for example:
如果我有两个页面,分别是page1.php
和page2.php
,我需要在./code.php
中操纵不同的变量和函数,include()
是否创建./code.php
的副本,或者实质上是链接回到实际页面./code.php
?
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
?
推荐答案
如果您include
文本文件,它将在文档中显示为文本.
If you include
a text file, it will show as text in the document.
include
不的行为类似于复制粘贴:
include
does not behave like copy-paste:
test.php
<?php
echo '**';
$test = 'test';
include 'test.txt';
echo '**';
?>
test.txt
echo $test;
上面的示例将输出:
**echo $test;**
如果要包含PHP文件,则仍需要具有PHP标签<?php
和?>
.
If you are going to include PHP files, you still need to have the PHP tags <?php
and ?>
.
此外,通常不会在include
之后加上括号,如下所示:
Also, normally parentheses are not put after include
, like this:
include 'test.php';
这篇关于在PHP中,include()到底如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!