php引入文件的方法有哪些-PHP问题

资源魔 31 0

PHP中引入文件的办法有:include、require、include_once、require_once。

区分引见:

include以及require

include有前往值,而require不前往值。

include正在加载文件失败时,会天生一个正告(E_WARNING),正在谬误发作后剧本持续执行。以是include用正在心愿持续执行并向用户输入后果时。

//test1.php
<?php
include './tsest.php';
echo 'this is test1';
?>

//test2.php
<?php
echo 'this is test2\n';
function test() {
    echo 'this is test\n';
}
?>

//后果:
this is test1

require正在加载失败时会天生一个致命谬误(E_COMPILE_ERROR),正在谬误发作后剧本中止执行。普通用正在后续代码依赖于载入的文件的时分。

//test1.php
<?php
require './tsest.php';
echo 'this is test1';
?>

//test2.php
<?php
echo 'this is test2\n';
function test() {
    echo 'this is test\n';
}
?>

后果:

1804f2f77b1a8f7c3e7373eebc1812d.png

include以及include_once

include载入的文件没有会判别能否反复,只需有include语句,就会载入一次(即便可能呈现反复载入)。而include_once载入文件时会有外部判别机制判别后面代码能否曾经载入过。

这里需求留意的是include_once是依据后面有没有引入相反门路的文件为判别的,而没有是依据文件中的内容(即两个待引入的文件内容相反,应用include_once仍是会引入两个)。

//test1.php
<?php
include './test2.php';
echo 'this is test1';
include './test2.php';
?>

//test2.php
<?php
echo 'this is test2';
?>

//后果:
this is test2this is test1this is test2


//test1.php
<?php
include './test2.php';
echo 'this is test1';
include_once './test2.php';
?>

//test2.php
<?php
echo 'this is test2';
?>

//后果:
this is test2this is test1


//test1.php
<?php
include_once './test2.php';
echo 'this is test1';
include './test2.php';
?>

//test2.php
<?php
echo 'this is test2';
?>

//后果:
this is test2this is test1this is test2


//test1.php
<?php
include_once './test2.php';
echo 'this is test1';
include_once './test2.php';
?>

//test2.php
<?php
echo 'this is test2';
?>

//后果:
this is test2this is test1

require以及require_once:同include以及include_once的区分相反。

更多相干教程请拜访资源魔。

以上就是php引入文件的办法有哪些的具体内容,更多请存眷资源魔其它相干文章!

标签: php 文件 php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!