-5

This is my error setting

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL|E_PARSE);

Now I write this test code:

helloooooo;

And It shows me:

Use of undefined constant helloooooo ...

So far so good.

Now I remove the semicolon from end of my so-called code:

helloooooo

Shows nothing. It shows a white page.

In my experience whenever I see a white page there is a syntax error so I look for a typo.

The question is why doesn't PHP help in this case?

Someone said :

Syntax checking is done before executing any code. So it can't execute the ini_set() functions if there's a syntax error.

How about PHP interpreter applies settings first then executes the rest of code?

JavaScript interpreter can detect the same error in runtime. Just try this:

<script>
helloooooo
</script>

Now go to Firefox => Tools=> WebDeveloper => WebConsole

ReferenceError: helloooooo is not defined

Vahid2023
  • 863
  • 3
  • 11
  • 33

1 Answers1

1

PHP's default error reporting mechanism (might) suppress error output. If you're trying to turn it on at runtime using the error_reporting function, then PHP will first have to successfully parse the file, then execute it to set that new desired error reporting level. That fails when there are syntax errors, because then PHP can't successfully parse and execute the file to change the error reporting level. The syntax error is getting suppressed because… well… it couldn't change the error reporting level yet.

Javascript doesn't have that, it outputs any and all errors it encounters.

The reason PHP likes to suppress errors is because its running on the server, and internal server debug details shouldn't be shown on public web pages. In Javascript that is moot, since it's running in the browser anyway, and the errors are reported to the console, which regular users don't see. Moreover, PHP does log all errors to a log file, so the same way you go looking for errors in Javascript, you could explicitly go looking for them in PHP's log file as well.

deceze
  • 510,633
  • 85
  • 743
  • 889