Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Syntax Error in Setting Global Variable

Ok, so my PHP is, to say the least, horrible. I inherited an application and am having to fix errors in it from someone that wrote it over 7 years ago. When I run the page, there is no return, so I checked the logs to see the error and here is what i get:

PHP Parse error: syntax error, unexpected '=', expecting ',' or ';' in /httpdocs/cron123/purge.php on line 4

Here is the code:

<?
ob_start();

global $siteRoot    =   '/httpdocs/';
global $reportRoot  =   '/reports/';
include('billing1.php');    

$date='Purge report for: ' .date('M d, Y \a\t g:i a'); ?>

<html>
<head><title><?=$date?></title></head>
<body>

<?php       
    $account = new billing();
    $ftresult = $account->purge();
    new dBug($ftresult);        
    echo "successfully wrote";
?>
</body>
<? 
    $filename = "purge_report_" . date('y.m.d_\a\t_g_i_a') . ".html";
    $loc = $reportRoot . 'purge_reports/';
    $f = $loc . $filename;

    $fp = @fopen($f, 'w'); 
    @fwrite($fp, ob_get_contents());
    @fclose($fp);

    ob_end_flush(); 
?>
like image 598
Russ Bradberry Avatar asked Jul 17 '09 22:07

Russ Bradberry


3 Answers

global is a keyword that should be used by itself. It must not be combined with an assignment. So, chop it:

global $x;
$x = 42;

Also, as Zenham mentions, global is used inside functions, to access variables in an outer scope. So the use of global as it is presented makes little sense.

Another tip (though it will not really help you with syntax errors): add the following line to the top of the main file, to help debugging (documentation):

error_reporting(E_ALL);
like image 162
Stephan202 Avatar answered Sep 22 '22 11:09

Stephan202


The global keyword is used inside of functions to declare that they will use a globally defined variable, not to define one. Just remove the word global, and if you need those values in functions, add:

global $a;

...to the start to the function.

like image 22
markh Avatar answered Sep 20 '22 11:09

markh


See here. global is a modifier which means the variable comes from the global scope. It should just be

<?
ob_start();

$siteRoot        =       '/httpdocs/';
$reportRoot      =       '/reports/';

and in functions which use them (but you don't have any in this page)

function f() {
  global $siteRoot, $reportRoot;
  ...
}
like image 36
Alexey Romanov Avatar answered Sep 21 '22 11:09

Alexey Romanov