I've spent a bit searching The Google as well as SO and have been unable to find an answer for this simple question:
E_COMPILE_WARNING
in PHP?The PHP manual entry on the subject docs says of E_COMPILE_WARNING
:
Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
But I'm not sure what this would constitute. What's the difference between a regular E_WARNING
and a warning raised by the Zend Scripting Engine? Could someone please explain and provide a code snippet if applicable?
A fatal error is another type of error, which is occurred due to the use of undefined function. The PHP compiler understands the PHP code but also recognizes the undefined function. This means that when a function is called without providing its definition, the PHP compiler generates a fatal error.
A PHP error isn't a single thing, but comes in 4 different types: parse or syntax errors. fatal errors. warning errors. notice errors.
E_ERROR. 1. A fatal run-time error, that can't be recovered from. The execution of the script is stopped immediately. E_WARNING.
Basically, the difference between an E_WARNING
and an E_COMPILE_WARNING
is that E_COMPILE_WARNING
is generated while the script is still compiling.
E_COMPILE_WARNING
is similar to E_COMPILE_ERROR
in that it is generated during compile time, but an E_COMPILE_WARNING
does not prevent script execution the way E_COMPILE_ERROR
does. Compare it to the the relation between E_ERROR
and E_WARNING
, where the former halts execution, and the latter allows execution to continue.
For example, the following code generates an E_COMPILE_WARNING
:
<?php
echo "\n";
echo "Hello World";
echo "\n\n";
var_dump(error_get_last());
declare(foo='bar');
?>
output:
Warning: Unsupported declare 'foo' in e_compile_warning.php on line 6
Hello World
array(4) {
["type"]=>
int(128)
["message"]=>
string(25) "Unsupported declare 'foo'"
["file"]=>
string(124) "e_compile_warning.php"
["line"]=>
int(6)
}
Notice how the warning is displayed before the other output (even though "Hello World" came first in the source), and the var_dump
statement on line 5 references an error that occurs on line 6. PHP compiles the script, doesn't like declare(foo='bar');
, but goes back and executes the script anyway (as opposed to an E_COMPILE_ERROR
like $this = 2;
, which would stop execution (and compilation) immediately).
Hope this helps!
You can produce the output warning directly using the following code:
$cmd = "declare(foo='bar');";
eval($cmd);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With