PHP:包含使用eval的等价物 [英] PHP: Equivalent of include using eval

查看:102
本文介绍了PHP:包含使用eval的等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果代码相同,则之间似乎存在差异:

If the code is the same, there appears to be a difference between:

include'external.php';

eval('?>'。file_get_contents( 'external.php')。'<?php');

有什么区别?有人知道吗?

What is the difference? Does anybody know?

我知道两者不同,因为包括工作正常, eval 给出错误。当我最初问这个问题时,我不确定它是否在所有代码上或者仅在我的代码上出错(并且因为代码是 eval ed,这很难找出错误意味着什么)。然而,在研究了答案之后,事实证明你是否得到错误并不依赖于 external.php 中的代码,但确实取决于你的php设置(准确地说是 short_open_tag )。

I know the two are different because the include works fine and the eval gives an error. When I originally asked the question, I wasn't sure whether it gave an error on all code or just on mine (and because the code was evaled, it was very hard to find out what the error meant). However, after having researched the answer, it turns out that whether or not you get the error does not depend on the code in the external.php, but does depend on your php settings (short_open_tag to be precise).

推荐答案

经过一些更多的研究我发现自己出了什么问题。问题在于<?php 是一个短开始标记,因此仅在 short_open_tag 设置为1(在php.ini或类似的效果)。正确的完整标签是<?php ,它在第二个p之后有一个空格。

After some more research I found out what was wrong myself. The problem is in the fact that <?php is a "short opening tag" and so will only work if short_open_tag is set to 1 (in php.ini or something to the same effect). The correct full tag is <?php, which has a space after the second p.

因此相应的包含是:

eval('?>' . file_get_contents('external.php') . '<?php ');

或者,您可以将开头标记全部保留在一起(如下面的评论中所述):

Alternatively, you can leave the opening tag out all together (as noted in the comments below):

eval('?>' . file_get_contents('external.php'));

我原来的解决方案是添加分号,这也有效,但如果你看起来不那么干净问我:

My original solution was to add a semicolon, which also works, but looks a lot less clean if you ask me:

eval('?>' . file_get_contents('external.php') . '<?php;');

这篇关于PHP:包含使用eval的等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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