php eval() 代码中出现意外的 $end

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6142550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:28:31  来源:igfitidea点击:

Unexpected $end in eval()'d code

phpevalsyntax-error

提问by andrewtweber

I hate to ask such a specific question, but I'm getting an error I can't figure out. This is in a cron job which runs on the hour. I'm creating an array of tasks, each of which has a date check which is supposed to be eval()'d.

我讨厌问这样一个具体的问题,但我遇到了一个我无法弄清楚的错误。这是在每小时运行的 cron 作业中。我正在创建一个任务数组,每个任务都有一个日期检查,应该是 eval()'d。

$todo = array();
$todo[] = array( "date('z')%3 == 0", "Task 1" );
$todo[] = array( "date('N') == 1", "Task 2" );
foreach( $todo as $task )
{
    if( eval($task[0]) ) {
        echo $task[1];
    }
}

For some reason the eval() line is giving me this error. Note that I am getting this error for both tasks.

出于某种原因, eval() 行给了我这个错误。请注意,我在这两个任务中都收到此错误。

Parse error: syntax error, unexpected $end in /file.php(21) : eval()'d code on line 1

Any suggestions? I tried searching for this but couldn't find anything. Thank you.

有什么建议?我尝试搜索此内容,但找不到任何内容。谢谢你。

回答by mario

evalonly accepts statements, not expressions. You need to convert your tests with:

eval只接受语句,不接受表达式。您需要将测试转换为:

if (eval("return $task[0];")) {

回答by Tomero Indonesia

You are missing ';' on end of string evaluation. Eval function able to process statements or expressions.

你缺少';' 在字符串评估结束时。Eval 函数能够处理语句或表达式。

Example:

例子:

$value = 7;
eval("$value+=2;");
echo $value;