What's __halt_compiler in PHP for?

php

Solution

Assume you have one script with some php code and lots and lots of binary clutter.

<?php doStuff(); __halt_compliler(); [BIG_BINARY_MESS]

then you want the compiler to NOT try to parse the binary because if there is `<?` somewhere in the binary it would break.

The point is being able to just ship one file with binary data and php code.

For a little example see this blog post

So you want not only to stop the `execution` of a script (like `exit()` would) but to stop the `parsing` so that you can have "invalid syntax" at the end of file and php still can execute the first part.

Another example:

This will get parsed as valid php and execute just fine:

<?php $a = 1; echo $a; __halt_compiler(); §RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

To access the data:

<?php
$file = fopen(__FILE__, 'rb');
// Go to the end of the __halt_compiler();
fseek($file, __COMPILER_HALT_OFFSET__);
echo stream_get_contents($file);
__halt_compiler(); §RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

This will output `§RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();`

Problem

From the manual: ``` void __halt_compiler ( void ) ``` This function halts the execution of the compiler. This can be useful to embed data in PHP scripts, like the installation files. Note: `__halt_compiler()` can only be used from the outermost scope. Can anyone provide an actually case where this function is useful?

Original source