views:

75

answers:

2

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?

+4  A: 

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

Amber
Will this ensure that it doesn't even parse the rest of the file?
SLaks
Correct........
The Pixel Developer
Yes... the `exit` keyword specifically tells PHP to stop parsing, flush what output it already has, and return control back to what invoked it.
Amber
A: 

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().

Pascal MARTIN