简体   繁体   中英

PHP execution path (parser)

See the following snippet:

<?php
  die("----die----");

  sfafsadffas

  echo "foo";
  echo "bar";
?>

The result is:

Parse error: parse error in test.php on line 6.

Which is a bit unexpected, I would have thought we should get ----die---- .

Now see the following:

<?php
  die("----die----");

  echo "foo";
  echo "bar";

  sfafsadffas
?>

The result is:

----die----

What exactly is going on here?

The echo is indirectly effecting your program: besides printing to screen/browser the echo is doing flush to the output buffer which tries to print all the output that was "aggregated" so far. Since there's a syntax error before the flush it will be discovered at this point. You can reproduce the same error by doing:

<?php
die("----die----");

sfafsadffas
flush();
?>

But, if you'll flush the output buffer before the parser reaches the syntax error - the die will be executed.

<?php
die("----die----");

flush();
sfafsadffas
?>

will output ----die---- and then the parser will stop execution because it died and you won't reach the line that causes a syntax error.

The first thing PHP does when executing a file is to run a syntax check looking for obvious errors. This includes missing ; semicolons, mismatched braces {} , etc.

The first snippet fails this first test:

<?php
die("----die----");

sfafsadffas  // <-- no semicolon between this line of code and the next,
             //     so you get a 'parse error' before the file even gets run
echo "foo";
echo "bar";
?>

The second snippet survives the syntax check because of a little "feature" of PHP: the parser counts ?> as a semicolon. So when you run:

<?php
die("----die----");

echo "foo";
echo "bar";

sfafsadffas
?>

The syntax check says "OK" and PHP proceeds to run the file. And the file would in fact be perfectly valid if sfafsadffas was defined sometime earlier in the file.

And then because of your die , the code never gets down to line 7.

The first fails, because the parser see's the invalid text and errors out.

The second may or may not fail depending on your PHP interpreter, it may not fail because the pre-processor (or parser) is ignoring extraneous data at the end of the file.

the sfafsadffas is considered as a constant here. In example 1, put a semicolon after sfafsadffas and you are using a constant a normal way so it works. In example 2, You do no need to have a semicolon separate last line of code see this

Throws Parse error

Parse error: syntax error, unexpected $end on line 7

Please check

http://writecodeonline.com/php/

Example 1:

It should not execute. Because the coding will run line by line. The invalid text will stop remain executions. Finally nothing will execute and execution will be fail.

Example 2:

By the reason of line by line execution the both echo statements will execute ,but finally the execution status will be fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM