What is the difference between die, exit and __halt_compiler?

14

What are the main differences between die , exit and __halt_compiler ?

    
asked by anonymous 02.07.2015 / 17:46

2 answers

19

About die and exit

Between die and exit , there is no difference, they are the same, according to the manual. One is nickname for the other.

PHP Manual for exit :

  

Note: This language construct is equivalent to die ().

PHP Manual for die :

  

This language construct is equivalent to exit ().

About __halt_compiler :

There is a PHP mechanism that allows you to embed snippets of data (including binary data) that will not even be interpreted by the PHP interpreter. Unlike the exit command, which stops script execution, but does not interrupt the PHP parser, there is the command __halt_compiler , which ignores absolutely everything that is below the point where it was called.

The use of this feature is unusual, but allows, for example:

Creating self-extracting PHP scripts (attaches a data package to the script) Creation of files that require authentication for access, but are in a public area on the Web Store meta-information about the PHP script. Ideally, the script itself can "read" what comes after the point at which __halt_compiler was called. For this, there is a special constant called __COMPILER_HALT_OFFSET__ , which stores the position of the first byte after the __halt_compiler() call.

See a simple example:

<?php

// Abrir o proprio arquivo para leitura
$f = fopen(__FILE__, 'r');

// Posicionar o cursor na posicao do __halt_compiler() + 1
// (+1 pois existe a quebra de linha)
fseek($f, __COMPILER_HALT_OFFSET__ + 1);

// Ler todo conteudo do cursor ate o final do arquivo e fecha-lo
$conteudo = stream_get_contents($f);
fclose($f);

// Fazer algo com o conteudo
echo $conteudo;
exit(0);


__halt_compiler();
tudo que vem aqui é ignorado!
blá blá blá

Source over __halt_compiler()

    
02.07.2015 / 19:10
10

The die() and exit() functions are equivalent, according to the documentation , and serve to stop script execution by displaying an additional message.

While __halt_compiler() has a different purpose. It is used to quit the compiler parse after a certain point in the file. If applied to code debugging, __halt_compiler() will ignore syntax errors from a given file snippet, which does not happen with die() and exit() .

Consider the following script:

<?php

echo 'Estou debugando esse script';

die('bye_bye');

Aqui nessa parte existem erros de sintaxe!

It will generate the following error:

  

PHP Parse error: syntax error, unexpected 'in' (T_STRING) in C: \ www \ chiqueirinho \ halt.php on line 7

     

Parse error: syntax error, unexpected 'ese' (T_STRING) in C: \ www \ chiqueirinho \ halt.php on line 7

Now, changing die to __halt_compiler() :

<?php

echo 'Estou debugando esse script';

__halt_compiler();

Aqui nessa parte existem erros de sintaxe!

The output will be as follows:

  

I am debugging this script

The __halt_compiler() can also be used for creating templates, separating the application code from the layout thanks to constant __COMPILER_HALT_OFFSET__ that returns the exact point where __halt_compiler() is located in the file.

We have here an example of this technique used in templates, however it is more common to find this type of template construction with files separated as has been applied in this example .

    
02.07.2015 / 19:19