Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What sort of issue would generate an E_COMPILE_WARNING in PHP?

Tags:

php

I've spent a bit searching The Google as well as SO and have been unable to find an answer for this simple question:

What sort of issue would generate an 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?

like image 570
rdlowrey Avatar asked Jan 07 '12 07:01

rdlowrey


People also ask

What is the PHP Fatal Error type?

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.

What is PHP error level?

A PHP error isn't a single thing, but comes in 4 different types: parse or syntax errors. fatal errors. warning errors. notice errors.

What is the meaning of E_error and E_warning?

E_ERROR. 1. A fatal run-time error, that can't be recovered from. The execution of the script is stopped immediately. E_WARNING.


2 Answers

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!

like image 196
Kevin Arthur Avatar answered Nov 09 '22 10:11

Kevin Arthur


You can produce the output warning directly using the following code:

$cmd = "declare(foo='bar');";
eval($cmd);
like image 24
NetAktiv Avatar answered Nov 09 '22 12:11

NetAktiv