doukuiqian9911 2017-01-18 12:20
浏览 65
已采纳

如果用它们指定的文件包含语法错误,“include”和“require”的行为方式是否相同? [关闭]

Suppose, I have a PHP file that contains some PHP code. This file also contains few syntactical errors.

If I try to add the code of this file with syntactical errors to another PHP file using either "include" or "require" by specifying the file name with correct path then what will happen during execution?

Will the code execution continue with a warning like it does when "include" fails to load the specified file or will the code execution stop in both scenarios with FATAL ERROR?

I want to know what will happen in both cases viz. "include" and "require" when I try to include a file with syntactical errors to the currently executing PHP file?

Note : I know what is "include" and "require". I also know the difference between them when the specified file is missing. But my question is different as it is related to the syntactical error in a file to be included in both "include" and "require". So, please don't mark my question as duplicate as I have not found a single question asking the thing I've asked.

If someone could explain me with working code examples and the working fiddle, it would be great and easier for me to understand the concept more clearly.

  • 写回答

3条回答 默认 最新

  • dongquanjie9328 2017-01-24 22:28
    关注

    In both scenarios, a parse error (E_PARSE) will be raised. You cannot catch this error and it is always fatal.

    When you call include on a file that cannot be read at all, a runtime warning is produced (E_WARNING). Calling require on a similar file raises the (uncatchable, fatal) E_COMPILE_ERROR error.

    However, if the file exists, PHP will attempt to parse it in the context of the calling script, as described in the manual. If there is a syntax error, that will raise E_PARSE and will terminate execution. The same applies to any other uncatchable error.

    If you need for some reason to determine whether a PHP file has valid syntax, you may be able to use the parsekit_compile_file function from the Parsekit PECL extension. Keep in mind this is experimental and not included in core PHP.

    To demonstrate, consider three PHP files:

    test_require.php:

    <?php
    require 'include_me.php';
    

    test_include.php:

    <?php
    include 'include_me.php';
    

    include_me.php:

    <?php
    syntax error
    

    Running php test_require.php or php test_include.php will result in:

    Parse error: syntax error, unexpected 'error' (T_STRING) in /path/to/include_me.php on line 2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据