Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected $end in eval()'d code

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.

$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.

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.

like image 999
andrewtweber Avatar asked May 26 '11 17:05

andrewtweber


People also ask

What is parse error syntax error unexpected end of file?

This error is typically caused by a missing } used in PHP to denote content belonging to a WHILE, IF, or FOR loop. If you decide you no longer want to "do that", be very careful to not remove the } as I have done here.

What is eval function in PHP?

Definition and Usage. The eval() function evaluates a string as PHP code. The string must be valid PHP code and must end with semicolon. Note: A return statement will terminate the evaluation of the string immediately. Tip: This function can be useful for storing PHP code in a database.


1 Answers

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

if (eval("return $task[0];")) {
like image 73
mario Avatar answered Sep 21 '22 16:09

mario