PHP 5.6.0RC3 is available


(PHP 5 >= 5.1.0)

__halt_compiler Detiene la ejecución del compilador


void __halt_compiler ( void )

Detiene la ejecución del compilador. Esto puede ser útil para embeber información en scripts de PHP, como en archivos de instalación.

La posición de byte del inicio de la información se puede determinar por la constante __COMPILER_HALT_OFFSET__, la cuál está definida sólo si hay un __halt_compiler() presente en el archivo.

Valores devueltos

No devuelve ningún valor.


Ejemplo #1 Un Ejemplo de __halt_compiler()


// abrir este archivo
$fp fopen(__FILE__'r');

// buscar el puntero de archivo a la información

// e imprimirlo

// el final de la ejecución del script
__halt_compiler(); la información de instalación (p.ejtargzPHPetc.)



__halt_compiler() sólo se puede usar desde el ámbito más externo.

add a note add a note

User Contributed Notes 7 notes

9 months ago
What zsh mentioned six years ago: "Also, it cannot be used in eval() -- it won't throw a syntax error, but it won't do anything either." is wrong. It can be used in eval() -- it will halt the eval.
ravenswd at gmail dot com
4 years ago
__halt_compiler is also useful for debugging. If you need to temporarily make a change that will introduce an error later on, use __halt_compiler to prevent syntax errors. For example:

if ( $something ):
// endif placed here for debugging purposes
// original location of endif -- would produce syntax error if __halt_compiler was not there
joey at gimo dot co dot uk
2 months ago
I don't exactly know what PHP is doing internally but I don't understand the sanity behind how in token_get_all __halt_compiler is handled.

This is actually valid there:
__halt_compiler/**/ /**/ /**/ /**/ /** */();raw

Normally it pops off just any three tokens so you can have even __halt_compiler***, __halt_compiler))), etc in token _get all.

The weird thing is that is also skips T_OPEN_TAG but in the context __halt_compiler runs in this tag should not be posible. Instead it will pick up < and ? as operators and php as a T_STRING.

It ignores the token at any point so this is also valid:
__halt_compiler()/**/ /**/ /**/ /**/ /** */;raw

When I test this with a php file rather than the tokeniser it works the same.

I can only conclude that PHP/__halt_compiler is pretty weird.

I think this is from attempting to weakly imitate the same syntax handling as in functions (I guess you can put comments/whitespace anywhere). I find it annoying and counter productive though.

Even this is valid:
__halt_compiler// comment\n();raw

A general problem that compound matters is that tokenise wont check whether or not syntax is valid (tokens against each other). When running as PHP you must have ();.
18 days ago
Joey, you're wrong saying that __halt_compiler have strange behavior. This structure works exactly the same as any other build in structure like empty or isset (even similarly to functions; at least in tokenizer level).

About T_OPEN_TAG - after one open tag is present you didn't expect other one in current php code section, so tokenizer try to handle this "thing" in other way and it's perfectly normal...
6 years ago
__halt_compiler() is a language construct and therefore cannot be used as a variable function.

Also, it cannot be used in eval() -- it won't throw a syntax error, but it won't do anything either.
7 months ago
This is not what people expect to see on manual pages, don't take this as example.
Ultimater at gmail dot com
5 years ago
Another possible usage, in addition to binary data and installers,
is using it to separate model, view and controller logic from one another:

class viewhelper{function render(){

//database logic goes here
//database logic goes here

//controller logic goes here
$view=new viewhelper;
$view->title="Test -> Ultimater's MVC example";
//controller logic goes here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<td>Hello <a href="<?=$this->base?>/showuser.php?id=<?=$this->userid?>
<td><a href="<?=$this->base?>/logout.php">Log Out</a></td>
<th><a href="<?=$this->base?>/usercp.php">UserCP</a></th>
<? if($this->authbar->modcp){ ?>
<td>&middot;</td><th><a href="<?=$this->base?>/modcp.php">ModCP</a></th>
<? } ?>
<? if($this->authbar->admincp){ ?>
<td>&middot;</td><th><a href="<?=$this->base?>/admincp.php">AdminCP</a></th>
<? } ?>
<table><tr><td>Hello Guest</td><td>[</td>
<td><a href="<?=$this->base?>/login.php">Log In</a></td>
<th><a href="<?=$this->base?>/register.php">Register</a></th>
<? } ?>
To Top