Php – syntax error near unexpected token `<'

cronPHP

I have set the php file in cron job on the parallel plesk 10.4.4 server.In notification email it is showing the errors.

      syntax error near unexpected token `<' 
     syntax error near unexpected token `newline'
     /newsletter_auto.php: line 2: `<html>'

the code of the newsletter_auto.php file is below

        <title>Market Trend Newsletter</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var abc=encodeURIComponent($("#newsletter").html());
    $.ajax({
            type:'POST',
            url:'newsletter_automation.php?abc='+abc,
            success:function(data){
             }
        });
});
</script>
    </head>
    <body>
<?php
    function checkForNULLString($str)
    {
        if($str == "" || $str == " " || $str == "  " || $str == "   " || $str == "    " || $str == "     " || $str == "       " || $str == null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }

    function cleanForDatabase($str)
    {
        $str =  preg_replace('/[^a-zA-Z0-9\s-_#@.,]/', '', $str);
        $str = mysql_real_escape_string($str);

        return $str;
    }

    function runSQLQuery($query,$mysqlConnect)
    {
        $result = mysql_query($query,$mysqlConnect);

        if(mysql_errno($mysqlConnect) != 0){
            echo "\n !<u>!ERROR:</u>mysqlConnect:".$mysqlConnect." <br/>query:".$query."<br/> Query.result:" . mysql_errno($mysqlConnect) . ": " . mysql_error($mysqlConnect) . "\n";

            //Log the Query Error
            $result = mysql_query($query,$mysqlConnect); exit();

        }

        return($result);
    }

    function insertIconAdminLink($link,$name,$type,$text=NULL,$target="_self")
    {
        //print"<a href=\"".$link."\" onmouseout=\"Javascript:roll_over_switch('".$name."',false);\" onmouseover=\"Javascript:roll_over_switch('".$name."',true);\"><img name=\"".$name."\" id=\"".$name."\" border=\"0\" src=\"/images/".$type.".gif\" />".$text."</a>";

        print" <a class=\"iconLinkText\" href=\"".$link."\" target=\"".$target."\"><img name=\"".$name."\" id=\"".$name."\" border=\"0\" src=\"/images/".$type.".gif\" width=\"24\" height=\"24\" />".$text."</a> ";
    }

    <div id="newsletter"> <p>Stocks traded <?php echo $stock_direction; ?> today with the <?php echo $stock_index; ?> closing at <?php echo $stock_index_close; ?>, <?php echo $stock_direction; ?> <?php echo $stock_index_difference; ?> or <?php echo $stock_index_percentage_difference; ?> from the previous close. The number of NYSE advances were <?php echo $nyse_advances; ?>, the number of decliners was </div>
      </body>
      </html>

Please let me know how can I resolve this?

Best Answer

That is the error you would expect if you execute a php template file e.g. test.php under the bash script interpretor, for example if you create a basic php script file;

$ echo -e "<html>\n</body><?php echo 'test'?></body></html>" > ~/test.php
$ cat test.php 
<html>
</body><?php echo 'test'?></body></html>

and make it executable, and then try and execute it directly...

$ chmod +x test.php 
$ ./test.php 
./test.php: line 1: syntax error near unexpected token `newline'
./test.php: line 1: `<html>'

This is because the shell is trying to run the script under the default script interpreter, i.e. bash, its the equivalent of this;

$ bash test.php 
test.php: line 1: syntax error near unexpected token `newline'
test.php: line 1: `<html>'

at the very least, something like this would at least run the php script under the php;

$ /usr/bin/php test.php
<html>
</body>test</body></html>