PHP Syntax Error in Setting Global Variable

前端 未结 4 2172
野性不改
野性不改 2020-12-20 19:17

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

相关标签:
4条回答
  • 2020-12-20 20:04

    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;
      ...
    }
    
    0 讨论(0)
  • 2020-12-20 20:10

    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);
    
    0 讨论(0)
  • 2020-12-20 20:13

    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.

    0 讨论(0)
  • 2020-12-20 20:14

    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
    
    0 讨论(0)
提交回复
热议问题