1

Is a parse error an EXAMPLE of an error that OCCURS BEFORE THE SCRIPT IS EXECUTED?

https://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.

https://www.php.net/manual/en/function.set-error-handler.php

If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.

My doubt is because in 2 cases PHP mentions the term "before executing".

  • 3
    The PHP interpreter has to load and parse the file before it can be executed. So for example a simple missing semi-colon will break the parser, and it will never get to the execution step. – aynber Nov 01 '22 at 14:18
  • hi @aynber, so a parse error cannot be handled even with a try-catch? –  Nov 01 '22 at 14:22
  • 2
    What documentation says is that you cannot use PHP code to change settings if: 1) The script is not valid so it's unable to run. 2) The setting you want to change affects something that already finished when the script starts running. – Álvaro González Nov 01 '22 at 14:24
  • 2
    No, because it won't get that far. The PHP interpreter doesn't read and execute the script line by line, it has to load the whole script then parse it. Once it knows it's in a functional state, only then will it attempt to execute it. A try/catch block would be part of the execution, but a parse/syntax error breaks the readability. – aynber Nov 01 '22 at 14:28
  • Ok, thanks guys, excellent answers, +1 to both of you. One last question, so is a parse error a FATAL ERROR? –  Nov 01 '22 at 14:32
  • 1
    Correct, because you cannot recover from it. – aynber Nov 01 '22 at 14:35
  • 1
    @aynber Small correction: it is not an *interpreter* which is parsing the file, it is a *compiler*; it just happens to be an automatic compiler, not one that you invoke separately. That's kind of important, since an interpreter is generally defined as something which *doesn't* look at a whole unit of code in advance. – IMSoP Nov 01 '22 at 14:39
  • 1
    @IMSoP I've heard it both ways, and even the [question here seems to say it's both](https://stackoverflow.com/questions/1514676/is-php-compiled-or-interpreted). I think I just get "interpreter" stuck in my head most of the time. – aynber Nov 01 '22 at 14:46
  • 2
    @aynber Indeed PHP has both: a compiler looks at the source code and generates opcodes; then an interpreter runs over those opcodes. Whether that makes the language as a whole "compiled" is a matter of debate and somewhat arbitrary definitions. But it's the *compiler* that is going to give you Parse Errors; if it was an interpreter, it would emit them as it reached each line or statement, not for the whole file. It felt like a useful enough distinction in this case to point out. – IMSoP Nov 01 '22 at 14:48

1 Answers1

1

PHP is not a directly interpreted language: your source code is processed first by a compiler. Specifically, when you tell PHP to execute a file, or include it with include/require, the entire file is parsed and compiled to an intermediate representation called "OpCodes". Only once the entire file has been compiled does any code in that file get executed (by running the OpCodes through an interpreter).

For that compilation to succeed, the syntax of the entire file needs to be correct. If it is not, a Parse Error will be generated.

If you are including a file, that error can be caught by the file which is calling include or require. But if the file being compiled is the one directly invoked by the web server or command-line command, there is no code "outside" that file which could catch the error, or change the settings relating to its display or handling.

When any error is not handled, PHP's last resort is simply to end the process - this is what is referred to as a "fatal error". In this case, the error can't be handled, so is always fatal; in most other cases, there is a chance to handle the error, and it only becomes fatal if you do not.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • excellent answer @IMSoP marked as a useful answer. "That's kind of important, since an interpreter is generally defined as something which doesn't look at a whole unit of code in advance." - because PHP parses the code in advance (because it is a compiler) it doesn't give you a chance to execute LITERALLY NO CODE? If it were an interpreter it would give a chance to execute some code, because an interpreter goes line by line (without giving a quick "glance") right? –  Nov 01 '22 at 15:48
  • 1
    @Daniel All generalisations are false, but yes some interpreters would allow you to make a syntax error on line 100 of a file, and only find out once lines 1 to 99 had been executed. Whether you think of that as "giving you a chance to run some code", or "telling you too late after some of the code has already run" is a matter of context. Also, as I mentioned, PHP in particular executes 1 *file* at a time, so you can run code before an `include` statement that reconfigures what will happen to a syntax error in the included file. – IMSoP Nov 01 '22 at 16:47