开发者

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

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,开发者_C百科 throws error

}
echo $number;

?>


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜