简体   繁体   English

停止php处理文件

[英]stop php processing file

Is there any way to make php stop processing a file and make it just work with the part it already parsed. 有没有办法让php停止处理文件,并让它只与它已经解析的部分一起使用。 I mean like this: 我的意思是这样的:

<some data here>
<?php phpinfo(); [IS THERE ANY THING I CAN PUT HERE] ?>
<more data>
[IS THERE ANY THING I CAN PUT HERE]
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

is there anyway to make php ignore data after the first php part? 无论如何,在第一个php部分之后让php忽略数据?

Another solution might be to use __halt_compiler : 另一种解决方案可能是使用__halt_compiler

Halts the execution of the compiler. 停止编译器的执行。 This can be useful to embed data in PHP scripts, like the installation files. 这对于在PHP脚本中嵌入数据非常有用,例如安装文件。

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. 数据开始的字节位置可以由__COMPILER_HALT_OFFSET__常量确定,该常量仅在文件中存在__halt_compiler()定义。

A typical usage is for Phar archives , when you need to embed PHP and (possibly binary) data into a single file, and the PHP code needs to have access to that data. 当您需要将PHP和(可能是二进制)数据嵌入到单个文件中时, Phar存档的典型用法是,PHP代码需要能够访问该数据。


And there is actually a difference : this code : 实际上有一点不同:这段代码:

blah blah
<?php phpinfo(); ?>
glop glop
<?php exit(); ?>
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

Gets me a Parse error: syntax error, unexpected T_STRING 给我一个Parse error: syntax error, unexpected T_STRING
(Probably because I have short_open_tag enabled, I suppose) (可能是因为我启用了short_open_tag ,我想)


While this one : 这一个:

blah blah
<?php phpinfo(); ?>
glop glop
<?php __halt_compiler(); ?>
<?HOW CAN I MAKE PHP NOT PARSE THIS?>

works OK -- this invalid PHP code being after the call to __halt_compiler() . 工作正常 - 这个无效的PHP代码在调用__halt_compiler()

I believe that all you need to do is to write return 0; 我相信你需要做的就是写return 0; (or any other value), PHP will stop parsing this file and return to whatever invoked it. (或任何其他值),PHP将停止解析此文件并返回到调用它的任何内容。 This is useful when you need to stop execution in one file but would not like to die() or exit() completely. 当您需要在一个文件中停止执行但又不想完全die()exit()时,这很有用。

您可以使用exit关键字告诉PHP完成文件的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM