What are the differences between die, Exit and __halt_compiler?

Asked

Viewed 7,005 times

16

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

  • 4

    Simple question that adds useful information to the community +1

2 answers

23


About die and exit

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

Manual PHP to Exit:

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

Manual PHP to die:

This language Construct is equivalent to Exit().

About __halt_compiler:

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

The use of this resource is uncommon, but allows, for example:

Auto-extractor PHP script creation (attaches a data package to the script) Creating files that require authentication for access, but stay 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 where the __halt_compiler. For this, there is a special constant called __COMPILER_HALT_OFFSET__, that stores the position of the first byte that is after the call of the __halt_compiler().

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 on __halt_compiler()

12

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

Already the __halt_compiler() has a different purpose. It is used to terminate the compiler parse after a certain point in the file. If applied for the code debug, the __halt_compiler() will ignore syntax errors from particular chunk of the file, which does not happen with the 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 'nessa' (T_STRING) in C: www Chiqueirinho Halt.php on line 7

Parse error: syntax error, Unexpected 'nessa' (T_STRING) in C: www Chiqueirinho Halt.php on line 7

Now by changing the die for __halt_compiler():

<?php

echo 'Estou debugando esse script';

__halt_compiler();

Aqui nessa parte existem erros de sintaxe!

The exit will be as follows:

I’m debugging this script

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

We have here an example of this technique used in templates, but it is more common to find this type of building templates with separate files, as it was applied in this example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.