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.
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
There is an article i have read before but it answers this quite well. http://andy-carter.com/blog/difference-between-include-and-require-statements-in-php
was asked the other day what’s the difference between include and require in PHP. They seemingly function the same but there is a significant difference.
First up, neither include or require are functions, they are constructs. It is therefore not necessary to call them using parentheses like include('file.php'); instead it is prefered to use include 'file.php'.
The difference between include and require arises when the file being included cannot be found: include will emit a warning (E_WARNING) and the script will continue, whereas require will emit a fatal error (E_COMPILE_ERROR) and halt the script. If the file being included is critical to the rest of the script running correctly then you need to use require.
You should pickup on any fatal errors thrown by require during the development process and be able to resolve them before releasing your script into the wild; however, you may want to consider using include to put in place a plan B if it’s not that straight-forward:-
<?php if (@include 'file.php') { // Plan A } else { // Plan B - for }
when 'file.php' cannot be included } In this example include is used to grab ‘file.php’, but if this fails we surpress the warning using @ and execute some alternative code. include will return false if the file cannot be found.
include_once and require_once behave like include and require respectively, except they will only include the file if it has not already been included. Otherwise, they throw the same sort of errors.
So to summarize include throws a warning and require throws a fatal error and ends the script when a file cannot be found. Simple as that!
But after this fact the php parser is built in in C++ and will parse until it it's an error the there is not 2 different parser's in PHP so they are parsed and error handled exactly the same after the file is found and loaded into the parser of php
This is where the php engine starts the handling for parsing files containing php instructions from functions
switch (EG(current_execute_data)->opline->extended_value) {
case ZEND_EVAL:
function = "eval";
is_function = 1;
break;
case ZEND_INCLUDE:
function = "include";
is_function = 1;
break;
case ZEND_INCLUDE_ONCE:
function = "include_once";
is_function = 1;
break;
case ZEND_REQUIRE:
function = "require";
is_function = 1;
break;
case ZEND_REQUIRE_ONCE:
function = "require_once";
is_function = 1;
break;
default:
function = "Unknown";
}
All of these functions then work the same way. so this is the evidence that PHP parses all includes, include_once, require, require_once & eval, exactly the same
I bet you already know that include
and require
are identical. Here's the main key words I think you're looking for.
require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will continue
So if you want to handle errors before continuing the process or algorithm, you may consider using require
because it's a strict command. But if it's nothing serious and will not cause problems to your linked codes or whatever (depending on your needs), you can use include
.
You can do a self testing of it. Examples and further explanations are included in the link below.