> Essays > coding > PHP-GlobalScope
> Essays > coding > PHP-GlobalScope
by ZweiBieren Using PHP Global pens in a mug

I took me years to understand the "global" keyword in PHP. Mostly because I kept making assumptions about it from its use in other languages. When I finally figure it out I added the following note to http://php.net/manual/en/language.variables.scope.php

Take to heart this hard-won rule:
  Declare AT THE TOP any variable that is to be global.
  Both at the top of the FILE
  AND at the top of any FUNCTION where it appears.
  
  Why AT THE TOP? So it is sure to be declared before use. 
  Otherwise a non-global version of the variable 
  will be created and your code will fail.
  
  Why at the top of a FUNCTION? Because otherwise 
  the function will refer only to its local version 
  of the variable and your code will fail.
  
  Why at the top of the FILE? 
  Because someday--a day that you cannot now imagine--you 
  will want to "include" the file. And when you do, 
  instances of the variable outside functions 
  will not go in the global scope and your code will fail. 
     (When the "include" is inside a calling function, 
     variables in the included file 
     go into the scope of the calling function.)
  

Example file where $x appears both outside and inside functions:

    |<!DOCTYPE html ...>
    |<html xmlns ...>
    |    <?php global $x; ?>
    |<head>
    |    Some html headers
    |    <?php
    |        $x = 1;
    |        function bump_x() {
    |            global $x;
    |            $x += 1;
    |        }
    |    ?>
    |</head>
    |<body>
    |    More html
    |    <:?php echo $x; bump_x(); ?>
    |    Yet more html.
    |</body>
    |</html>

Years later I had to return to some code I wrote before the above epiphany. Some problems had to be fixed by declaring a global varible in a function that did an include. To convince myself that this was unnecessary I wrote the following test where an initial PHP script includes a second which then includes a third.

file xxxDefiner.php defines $xxx and sets it to 1

<?php 
    global $xxx;
    $xxx = 1;
    echo "initial xxx is $xxx<br/>\n";
    echo "include xxxInclude.php and call incl()<br/>\n";
    include "xxxInclude.php";
    incl();
    echo "final xxx is $xxx\n";
?>

file xxxInclude.php  includes a file that refers to $xxx

<?php
    function incl() {
        echo "   incl() includes xxxReferer.php ";
        echo "and calls ref()<br/>\n";
        include "xxxReferer.php";
        ref();
    }
?>

file xxxReferer.php modifies the value of $xxx

<?php
    global $xxx;
    function ref() {
        global $xxx;
        echo "      ref() sees xxx as $xxx ";
        echo "and sets it to 3<br/>\n";
        $xxx = 3;
    }
?>

The output is

initial xxx is 1
include xxxInclude.php and call incl()
    incl() includes xxxReferer.php and calls ref()
        ref() sees xxx as 1 and sets it to 3
final xxx is 3 

Global $xxx is shared by the first and third files, but the second does not need to mention it.

 
Copyright © 2023 ZweiBieren, All rights reserved. Mar 4, 2023 20:03 GMT Page maintained by ZweiBieren