10

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?

2
  • 1
    Ok I'll bite... I'm curious, why would you want to? What are you trying to do exactly? Commented Dec 1, 2009 at 2:29
  • Do you want to output the rest of the file as raw text? Commented Dec 1, 2009 at 2:30

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

nice tip! Will the rest of the file be sent to the browser (not cropped/cut there)? If so this may speed up handling long pages with only little PHP code in the beginning ( __halt_compiler(); as last line), or pure HTML pages included by PHP (adding a <?php __halt_compiler(); ?> as first line).
5

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.

1 Comment

As far as I understand it doesn't really stop parsing the file but it stops executing it when it reaches return
3

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

4 Comments

Will this ensure that it doesn't even parse the rest of the file?
Yes... the exit keyword specifically tells PHP to stop parsing, flush what output it already has, and return control back to what invoked it.
PHP keeps compiling even after finding the exit, which is only executed after the compilation of the complete file. Pascal's answer is correct.
This answer is plain wrong. The exit() function only stop execution, not parsing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.