Home » Php » interpreted language – PHP throws error inside function even though the function is not executed on the page

interpreted language – PHP throws error inside function even though the function is not executed on the page

Posted by: admin July 12, 2020 Leave a comment

Questions:

If PHP is interpreted language(every line is executed as it is reached), how come it throws errors if the error occurs inside a function which is never executed?

Or may be I don’t get what interpreted means?

For e.g

<?php 

$number = 1;

function square(){

 $foo = 1  //missing semicolon, throws error

}
echo $number;

?>
How to&Answers:

Because its syntax is first parsed in an attempt to tokenize it, before the PHP interpreter can begin.

Answer:

Before the page is actually interpreted by PHP, it is run through a preprocessor. That picks up any errors in your page before it actually starts to run and preforms a whole bunch of other operations to prepare your script for interpretation.

This ensures the scripts syntax is correct before anything happens and is why PHP throws an error even through your function is never called.

Answer:

The problem has nothing to do with how PHP is executed (it’s bytecompiled, by the way…), but rather how the code is parsed. With that error there, the PHP parser is unable to build a working program in memory and notifies you of the syntax error.