• 技术文章 >php教程 >php手册

    php关于require和include的区别

    2016-05-25 16:46:19原创676
    include() 或 require() 函数,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容,除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的.

    include() 或 require() 函数,您可以在服务器执行 php 文件之前在该文件中插入一个文件的内容,除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的,include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行).

     
     
    welcome to my home page 
    

    some text

    三个文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件,这是 "default.php" 中的代码:

    welcome to my home page 
    

    some text

    require() 函数

    require() 函数与 include() 相同,不同的是它对错误的处理方式.

    include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行).

    如果在您通过 include() 引用文件时发生了错误,会得到类似下面这样的错误消息:

     
     
     
    

    错误消息:

    warning: include(wrongfile.php) [function.include]:
    failed to open stream:
    no such file or directory in c:homewebsitetest.php on line 5
     
    warning: include() [function.include]:
    failed opening 'wrongfile.php' for inclusion
    (include_path='.;c:php5pear')
    in c:homewebsitetest.php on line 5
     
    hello world!


    教程地址:

    欢迎转载!但请带上文章地址^^

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:include php require 区别
    上一篇:php程序必看优化之方法 下一篇:php获取复选框 文本框、密码域、隐藏域、按钮、文本域值
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 投票管理程序_php基础• 如何选择一本不让你后悔的电脑书–以PHP为例(1)• php实现将数组转换为XML的方法• 浅谈php扩展imagick• PHP单件模式和命令链模式的基础知识
    1/1

    PHP中文网