Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php引入文件的方法有哪些

php引入文件的方法有哪些

发布时间:2020-05-15   编辑:www.jquerycn.cn
jquery中文网为您提供php引入文件的方法有哪些等资源,欢迎您收藏本站,我们将为您提供最新的php引入文件的方法有哪些资源

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的区别相同。

更多相关教程请访问jquery中文网。

以上就是php引入文件的方法有哪些的详细内容,更多请关注jQuery中文网其它相关文章!

  • 本文原创发布jQuery中文网,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php引入文件的方法有哪些
    php网页标题在哪里修改
    php.ini在哪
    php工程师面试需要哪些方面
    PHP入门基础之引用文件学习笔记
    php7的版本和5的版本有哪些不同?
    PHP中include()与require()的区别有哪些
    php代码审计需要会php吗
    php命令行下相对路径问题的解决方法
    php表单提交特殊字符过滤方法

    [关闭]