PHP错误未定义的函数

时间:2013-05-13 20:15:46

标签: eval php

我收到错误未定义函数。

以下是代码:

$myvar = "@file_get_contents";

eval($myvar("http://someurlupdatehere.com"));

The error I get is: 

<b>Fatal error</b>:  Call to undefined function @file_get_contents() .. 

3 个答案:

答案 0 :(得分:3)

@file_get_contents()不是有效的PHP函数。您可以删除抑制运算符以使其工作:

$myvar = "file_get_contents";

如果仍需要使用抑制运算符,请执行:

eval('@' . $myvar("http://someurlupdatehere.com"));

或者甚至离开抑制运算符并将整行作为字符串传递:

$myvar = "@file_get_contents";
eval("$myvar('http://someurlupdatehere.com');");

请注意,使用抑制运算符和eval通常是一个坏主意。

答案 1 :(得分:1)

我想要做的事情看起来很奇怪。我倾向于说“不要使用eval,不要那样做!” ;)

无论如何,这里有正确的语法:

$myvar = "@file_get_contents";
eval("$myvar('http://someurlupdatehere.com');");

或者如果您需要变量中file_get_contents()的返回值(可能的话,请使用此:

$myvar = "@file_get_contents";
eval("\$file = $myvar('http://someurlupdatehere.com');");
echo $file;

答案 2 :(得分:0)

<?php
$myvar = "file_get_contents";

eval(@$myvar("http://someurlupdatehere.com"));
?>

也适用。抑制错误。