Php – Parse error when run by cron but not from command line

command linecronPHP

So I have a cronjob which executes a PHP script like so:

0 0 * * * /usr/local/bin/php -f /home/mysite/mysite.com/cronjobs/renewal_email.php

If I issue the same command from the command line it runs without error and running with the l flag shows no syntax errors. However cron emails the following:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR

The line in question is

$courses[$t->CourseNumber] = $t->course()->CourseName;

Does this limited amount of info raise any red flags? Anyone see why this should happen under cron but not from the command line?

// Edit to add DreamHost support response. Script is working with this change.

The default version of PHP on the server is PHP4. This is because the server uses the PATH settings rather than the Apache to specify which version is run. The path for PHP4 /usr/local/bin/php comes before the PHP5 path /usr/local/php5/bin/php so it always runs first when you type php from the shell.

To run PHP5 from the shell you need to specify the entire path:

/usr/local/php5/bin/php --version

The cron user will not use your .bash_profile path so you would need to specify the full path to PHP5 in each cron job.

Best Answer

Hm. The error message sounds like you're running a PHP5 script on a PHP4 interpreter. However, if you use the exact same path to the PHP binary, I can't see how this could happen.

Can you have the cron job do a phpinfo() and see what the output is?

For PHP 4, you'd have to rewrite the instruction:

$temp = $t->course(); 
$courses[$t->CourseNumber] = $temp->CourseName;