Search code examples
phpsymfonyphp-8symfony6

Check on undefined variable causes fatal error


I'm updating a very old website from a symfony 2 version to a symfony 6 version and I'm rewriting a bunch of the code. I'm encountering a problem when checking a variable that has not be defined. It causes a critical error when I'm expecting a simple warning. For example:

if($testvar)
   echo "test";

I expect this to create a Warning, but not a fatal error. I've also tested this code online with some php tools and it seems its not creating a fatal error.

If it can help here is the precise error I'm getting from Symfony:

CRITICA| REQUES Uncaught PHP Exception ErrorException: "Warning: Undefined variable $testvar"

I'm using Symfony6 with PHP8.1.5

Perhaps there is some configuration at PHP level to be done ? Like not considering warning as critical error ?

Thanks


Solution

  • I did find a simple solution: changing the error reporting inside php code did the trick error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_WARNING);

    I still got the warnings as logs, but its not stopping the scripts from running anymore