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

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

本文介绍了设置全局变量时出现PHP语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,至少我的PHP是可怕的。我继承了一个应用程序,并且不得不修复7年前写过它的人的错误。当我运行该页面时,没有返回,所以我检查了日志以查看错误,这里是我得到的:

以下是代码:

 <? 
ob_start();

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

$ date ='清除报告:'.date('M d,Y \a\ g:i a'); ?>

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

$ account = new billing();
$ ftresult = $ account-> purge();
新的dBug($ ftresult);
回显成功写入;
?>
< / body>
$ filename =purge_report_。日期('y.m.d_\a\t_g_i_a')。 .HTML;
$ loc = $ reportRoot。 purge_reports /;
$ f = $ loc。 $文件名;

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

ob_end_flush();
?>


解决方案

是应该由其自身使用的关键字。它不能与任务合并。所以,把它砍掉:

  global $ x; 
$ x = 42;

另外,正如提及, global 用于函数内部,用于访问外部变量范围。因此,使用 global 是没有意义的。

另一个提示(尽管它不会真的有帮助你有语法错误):将以下行添加到主文件的顶部,以帮助调试(

  error_reporting(E_ALL); 


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:

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

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

这篇关于设置全局变量时出现PHP语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:42