downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

highlight_file> <get_browser
[edit] Last updated: Fri, 26 Apr 2013

view this page in

__halt_compiler

(PHP 5 >= 5.1.0)

__halt_compiler Halts the compiler execution

Description

void __halt_compiler ( void )

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.

Return Values

No value is returned.

Examples

Example #1 A __halt_compiler() example

<?php

// open this file
$fp fopen(__FILE__'r');

// seek file pointer to data
fseek($fp__COMPILER_HALT_OFFSET__);

// and output it
var_dump(stream_get_contents($fp));

// the end of the script execution
__halt_compiler(); the installation data (egtargzPHPetc.)

Notes

Note:

__halt_compiler() can only be used from the outermost scope.



highlight_file> <get_browser
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes __halt_compiler - [5 notes]
up
1
ravenswd at gmail dot com
2 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:

<?php
if ( $something ):
  print
'something';
endif;  
// endif placed here for debugging purposes
__halt_compiler();
endif;  
// original location of endif -- would produce syntax error if __halt_compiler was not there
?>
up
3
Ultimater at gmail dot com
3 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:

<?php
class viewhelper{function render(){
eval(
file_get_contents(__FILE__,null,null,__COMPILER_HALT_OFFSET__));
}}

//database logic goes here
$row=array(
'loggedin'=>true,
'modcp'=>true,
'admincp'=>false,
'username'=>'Ultimater',
'userid'=>1234
);
//database logic goes here

//controller logic goes here
$view=new viewhelper;
$view->title="Test -> Ultimater's MVC example";
$view->base="http://".$_SERVER['HTTP_HOST'];
$view->username=$row['username'];
$view->userid=$row['userid'];
$view->authbar->loggedin=$row['loggedin'];
$view->authbar->modcp=$row['modcp'];
$view->authbar->admincp=$row['admincp'];
$view->render();
//controller logic goes here

__halt_compiler();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=htmlentities($this->title)?></title>
<style type="text/css">
a{color:#3333ff;text-decoration:none;}
a:hover{color:#0380fc;}
</style>
</head>
<body>
<?php
if($this->authbar->loggedin){
?>
<table><tr>
<td>Hello <a href="<?=$this->base?>/showuser.php?id=<?=$this->userid?>
"><?=htmlentities($this->username)?></a></td><td>[</td>
<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>
<? } ?>
<td>]</td>
</tr></table>
<?php
}else{
?>
<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>
<td>]</td></tr></table>
<? } ?>
</body>
</html>
up
-1
mael d0t nison 47 epitech d0t net
2 years ago
It's possible to use __halt_compiler() with included files.
Only the current file will be affected, so it could be a good way to make plugins which embed a controller and its view in the same file (like Ultimater at gmail dot com did).

Base.php
<?php
 
echo "Base 1\n";
  require
'Included.php';
  echo
"Base2 \n";
?>

Included.php
<?php
 
echo "Included 1\n";
  __halt_compiler();
  echo
"Included 2\n";
?>

Calling Base.php will display:
Base 1
Included 1
Base 2
up
-1
zsh
5 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.
up
0
gn_shallyNOSPAM at yahoo dot com
4 years ago
actually, __halt_compiler did something, even in eval function, that things is set the constant __COMPILER_HALT_OFFSET__ to the right value

as all of you might know, PHP will change content of some magic constant like __FUNCTION__, __CLASS__, etc based on the situation. so do the __COMPILER_HALT_OFFSET__ constant, is a file specific constant, just like the __FILE__ constant, the content will be changed based on wich file the __halt_compiler() reside.

try this:

/**** whatever.php ****/
<?php
eval('echo __FILE__."(".__COMPILER_HALT_OFFSET__.")\n"; __halt_compiler();');
echo
__FILE__."(".__COMPILER_HALT_OFFSET__.")\n"; __halt_compiler();

if (!isset(
$whatever))
{
 
$whatever = 'whatever';
  include
__FILE__;
}
__halt_compiler();
whatever

 
show source | credits | stats | sitemap | contact | advertising | mirror sites