设置全局变量时的PHP语法错误

时间:2022-06-28 23:20:25

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至少可以说是可怕的。我继承了一个应用程序,并且必须从7年前编写它的人那里修复错误。当我运行页面时,没有返回,所以我检查了日志以查看错误,这是我得到的:

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

PHP解析错误:语法错误,意外'=',期待','或';'在第4行的/httpdocs/cron123/purge.php中

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

4 个解决方案

#1


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

global是一个应该自己使用的关键字。不得与作业相结合。所以,砍下它:

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.

此外,正如Zenham所提到的,全局用于函数内部,以访问外部作用域中的变量。因此,所呈现的全球使用毫无意义。

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);

#2


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关键字来声明它们将使用全局定义的变量,而不是定义变量。只需删除全局一词,如果您需要在函数中使用这些值,请添加:

global $a;

...to the start to the function.

...到功能的开始。

#3


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

看这里。 global是一个修饰符,表示变量来自全局范围。它应该是

<?
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;
  ...
}

#4


You must use global without assignment, only a variable.

您必须使用不带赋值的全局,只能使用变量。

As you do not functions, there is no need for the global keyword at all:

由于您没有功能,因此根本不需要全局关键字:

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

If you need the variables in a function just add:

如果您需要函数中的变量,只需添加:

global $siteRoot;
global $reportRoot

#1


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

global是一个应该自己使用的关键字。不得与作业相结合。所以,砍下它:

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.

此外,正如Zenham所提到的,全局用于函数内部,以访问外部作用域中的变量。因此,所呈现的全球使用毫无意义。

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);

#2


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关键字来声明它们将使用全局定义的变量,而不是定义变量。只需删除全局一词,如果您需要在函数中使用这些值,请添加:

global $a;

...to the start to the function.

...到功能的开始。

#3


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

看这里。 global是一个修饰符,表示变量来自全局范围。它应该是

<?
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;
  ...
}

#4


You must use global without assignment, only a variable.

您必须使用不带赋值的全局,只能使用变量。

As you do not functions, there is no need for the global keyword at all:

由于您没有功能,因此根本不需要全局关键字:

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

If you need the variables in a function just add:

如果您需要函数中的变量,只需添加:

global $siteRoot;
global $reportRoot