Home » Php » stop php processing file

stop php processing file

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is there any way to make php stop processing a file and make it just work with the part it already parsed. I mean like this:

<some data here>
<?php phpinfo(); [IS THERE ANY THING I CAN PUT HERE] ?>
<more data>
[IS THERE ANY THING I CAN PUT HERE]
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

is there anyway to make php ignore data after the first php part?

How to&Answers:

Another solution might be to use __halt_compiler :

Halts the execution of the compiler.
This can be useful to embed data in
PHP scripts, like the installation
files.

Byte position of the data start can be
determined by the
__COMPILER_HALT_OFFSET__ constant
which is defined only if there is a
__halt_compiler() presented in the
file.

A typical usage is for Phar archives, when you need to embed PHP and (possibly binary) data into a single file, and the PHP code needs to have access to that data.

And there is actually a difference : this code :

blah blah
<?php phpinfo(); ?>
glop glop
<?php exit(); ?>
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

Gets me a Parse error: syntax error, unexpected T_STRING

(Probably because I have short_open_tag enabled, I suppose)

While this one :

blah blah
<?php phpinfo(); ?>
glop glop
<?php __halt_compiler(); ?>
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

works OK — this invalid PHP code being after the call to __halt_compiler().

Answer:

I believe that all you need to do is to write return 0; (or any other value), PHP will stop parsing this file and return to whatever invoked it. This is useful when you need to stop execution in one file but would not like to die() or exit() completely.

Answer:

You can use the exit keyword to tell PHP to finish processing the file.