PHP:相当于使用 eval 的包含
如果代码相同,则似乎存在以下差异:
If the code is the same, there appears to be a difference between:
包括'external.php';
和
eval('?>' .file_get_contents('external.php') .'<?php');
有什么区别?有人知道吗?
What is the difference? Does anybody know?
我知道这两者是不同的,因为 include
工作正常,而 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 eval
ed, 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.
因此,包含的适当等价物是:
As such the proper equivalent of the include is:
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;');
相关文章