Viewed   101 times

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 . 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 at 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_at_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(); 
?>

 Answers

1

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);
Friday, August 19, 2022
1

The memory usage is a paltry concern. It's much more important that the code be easy to follow and not have... unpredicted... results. Adding global variables is a VERY BAD IDEA from this standpoint, IMO.

If you're concerned about memory usage, the thing to do is

function doSomething (&$var1, &$var2,..) {
   ...
}

This will pass the variables by reference and not create new copies of them in memory. If you modify them during the execution of the function, those modifications will be reflected when execution returns to the caller.

However, please note that it's very unusual for even this to be necessary for memory reasons. The usual reason to use by-reference is for the reason I listed above (modifying them for the caller). The way to go is almost always the simple

function doSomething ($var1, $var2) {
    ...
}
Saturday, November 12, 2022
2

Well, the following is invalid syntax:

if ($this->isQuestion$q($query)){

Try this instead:

foreach ($questions as $q) {
    if ($result = $this->{'isQuestion' . $q}()) {
        return $result;
    }
}
return false;
Tuesday, September 20, 2022
 
gunner
 
2

You can't use a foreach in a echo statement! Just break them up like this:

echo '<script>
      $( document ).ready(function() {
      $("#Drafts > tbody:last").append(';

foreach($this->central as $key2 => $value2) {
    echo '<tr> 
            <td><a href='. $value2->Token .'>Edit</a></td>
            <td class=T id='. $value2-    >Token.'>'.htmlentities($value2->Title).'</td>
          </tr>';
}

echo  ');});</script>';
Monday, October 3, 2022
 
raam
 
5

basically you have a variable with the same name as your session. ex:

$_SESSION['var1'] = null;
$var1 = 'something';

which will reproduce this error. you can stop PHP from trying to find existing variables and warning you about them by adding these lines to your script:

ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);

these values can be set in php.ini or .htaccess as well

Sunday, December 25, 2022
 
fatmajk
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :