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条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数