PHP syntax error in setting global variable

Good, so my PHP is, to put it mildly, terrible. I inherited the application, and I have to fix errors in it from someone who wrote it more than 7 years ago. When I start the page, there is no return, so I checked the logs to see the error, and this is what I get:

PHP parsing error: syntax error, unexpected '=', pending ',' or ';' in / httpdocs / cron 123 / 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(); 
?>
+3
source share
4 answers

globalis a keyword that should be used on its own. It should not be combined with the appointment. So chop it up:

global $x;
$x = 42;

, Zenham , global , . global, , .

( ): , ():

error_reporting(E_ALL);
+7

global , , , . global, , :

global $a;

... .

+2

See here . globalis a modifier, which means that the variable comes from the global scope. It should be easy

<?
ob_start();

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

and in functions that use them (but you don’t have them on this page)

function f() {
  global $siteRoot, $reportRoot;
  ...
}
+2
source

You should use global without assignment, only a variable.

Since you are not executing functions, the global keyword is not needed:

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

If you need variables in a function, just add:

global $siteRoot;
global $reportRoot
0
source

Source: https://habr.com/ru/post/1712906/


All Articles