php加载文件的方法是什么

发布时间:2021-05-27 09:45:46 作者:小新
来源:亿速云 阅读:121

这篇文章主要介绍php加载文件的方法是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

php加载文件的方法:1、使用“include '文件名'”语句加载;2、使用“require '文件名'”语句加载;3、使用“include_once '文件名'”语句加载;4、使用“require_once '文件名'”语句加载。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

PHP中有四个加载文件的语句:include、require、include_once、require_once

基本语法

require:require函数一般放在PHP脚本的最前面,PHP执行前就会先读入require指定引入的文件,包含并尝试执行引入的脚本文件。require的工作方式是提高PHP的执行效率,当它在同一个网页中解释过一次后,第二次便不会解释。但同样的,正因为它不会重复解释引入文件,所以当PHP中使用循环或条件语句来引入文件时,需要用到include。

include:可以放在PHP脚本的任意位置,一般放在流程控制的处理部分中。当PHP脚本执行到include指定引入的文件时,才将它包含并尝试执行。这种方式可以把程序执行时的流程进行简单化。当第二次遇到相同文件时,PHP还是会重新解释一次,include相对于require的执行效率下降很多,同时在引入文件中包含用户自定义函数时,PHP在解释过程中会发生函数重复定义问题。

require_once / include_once:分别与require / include作用相同,不同的是他们在执行到时会先检查目标内容是不是在之前已经导入过,如果导入过了,那么便不会再次重复引入其同样的内容。

相互区别

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

载入时执行过程

1. 从include(require)语句退出php脚本模式(进入html代码模式)

2. 载入include语句所设定的文件中的代码,并尝试执行

3. 退出html模式,重新进入php脚本模式,继续后面脚本程序的执行

//test1.php
<html>
<body>
主文件开始位置:
<?php
    echo "<br> 主文件中位置 A";
    include "./test2.php";    //要载入的文件
    echo "<br> 主文件中位置 B";
?>
<br> 主文件结束位置
</body>
</html>

//test2.php
<br> 被载入文件位置 1
<?php
echo "<br> 被载入文件位置 2";
?>
<br> 被载入文件位置 3

结果:

php加载文件的方法是什么

分析:

php加载文件的方法是什么

php的框架有哪些

php的框架:1、Laravel,Laravel是一款免费并且开源的PHP应用框架。2、Phalcon,Phalcon是运行速度最快的一个PHP框架。3、Symfony,Symfony是一款为Web项目准备的PHP框架。4、Yii,Yii是一款快速、安全和专业的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的开源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能强大的PHP框架。

以上是“php加载文件的方法是什么”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. php手动加载视图文件
  2. PHP ThinkPHP 3.2.3 自动加载公共函数文件的方法

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

php

上一篇:wdCP面板怎么升级MySQL版本为5.6

下一篇:php如何实现301跳转的方法

相关阅读

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

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