PHP中的__halt_compiler是做什么用的? [英] What's __halt_compiler in PHP for?

查看:158
本文介绍了PHP中的__halt_compiler是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自手册:

void __halt_compiler ( void )

此函数停止编译器的执行.这对于将数据嵌入PHP脚本(例如安装文件)很有用.

This function halts the execution of the compiler. This can be useful to embed data in PHP scripts, like the installation files.

注意:__halt_compiler()只能在最外面的范围内使用.

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

任何人都可以提供此功能有用的实际情况吗?

Can anyone provide an actually case where this function is useful?

推荐答案

假设您有一个脚本,其中包含一些php代码以及很多二进制文件.

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.

关键是能够只运送带有二进制数据和php代码的一个文件.

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

有关示例,请参见因此,您不仅要停止脚本的execution(如exit()一样),而且还要停止parsing,以便您可以在文件末尾使用无效语法",并且php仍然可以执行第一部分.

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.

这将被解析为有效的php并执行得很好:

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

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

要访问数据:

<?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(); §$"§%"§$!!();

这将输出§RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

这篇关于PHP中的__halt_compiler是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆