php中如何引入文件?

发布时间:2020-05-19 15:03:11 作者:Leah
来源:亿速云 阅读:240

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';
}
?>

结果:

php中如何引入文件?

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引入文件的方法,图文详解容易学习,配合代码阅读理解效果更佳,非常适合初学者入门,如果想了解更多相关内容,欢迎关注亿速云行业资讯!

推荐阅读:
  1. 怎么引入bootstrap文件
  2. html中如何引入php

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 引入文件

上一篇:如何统计MySQL查询时间

下一篇:phpstudy搭建本地项目图片教程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》