0

i have written this code and I got the point of its first function. I was practicing while looking at this screenshot from w3schools.

screenshot from w3schools.com

I request only some good and clear explanation regarding the second and the third function.

I have edited the code according to the differences seen in the code and its results.

    <?php 
    $x = 5;
    $y = 10;
    function myTest1(){
        global $x, $y;
        $y = $x + $y;
        echo "test1 value using GLOBAL keyword INSIDE function is : $y <br>";
    }
    myTest1();
    echo "test1 value using GLOBAL keyword OUTSIDE function is : $y <br><br>";
    ?>

    <?php 
    $x = 5;
    $y = 10;
    function myTest2(){
        $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
        echo "test2 value using NO GLOBAL with GLOBALS[variable/index] keyword INSIDE function is nothing : $y <br>";
    }
    myTest2();
    echo "test2 value using NO GLOBAL with GLOBALS[variable/index] keyword INSIDE function is : $y <br><br>";
    ?>

    <?php 
    $x = 5;
    $y = 10;
    function myTest3(){
        global $x, $y;
        $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
        echo "test3 value using GLOBAL with GLOBALS[variable/index] keyword INSIDE function is : $y <br>";
    }
    myTest3();
    echo "test3 value using NO GLOBAL with GLOBALS[variable/index] keyword INSIDE function is : $y <br>";
    ?>
suryavansh
  • 191
  • 2
  • 10
  • i will try it..i did not know about it. thank you. – suryavansh Apr 05 '19 at 19:59
  • 1
    It's not clear what kind of explanation you expect / where the confusion might be. – mario Apr 05 '19 at 19:59
  • @RiggsFolly CR is for asking advice on rewriting code, not explanations of how the code works. – Barmar Apr 05 '19 at 20:01
  • @mario sir please. read the outputs inside the echo statements. – suryavansh Apr 05 '19 at 20:01
  • What don't you understand? When you've declared `$x` as global, `$x` is the same as `$GLOBALS['x']`. That's all there is to it. – Barmar Apr 05 '19 at 20:03
  • Have you read the manual section https://www.php.net/manual/en/reserved.variables.globals.php on this? Or is your issue with aliasing `global $y` and accessing it via `$GLOBALS[]` at the same time? – mario Apr 05 '19 at 20:03
  • @barmar. no. sir....i need the explanation regarding the second and the third function inner workings. as of ..why they are working different than the first one. as i understand that in first function i called the global scoped variables inside the function by using global keyword. but it is working differently in the second function ..which is not explained in the w3school site as why. – suryavansh Apr 05 '19 at 20:03
  • @barmar i n second function inside the function why is it not giving any value.....? as on the website it is written that "PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly." – suryavansh Apr 05 '19 at 20:05
  • @mario i am reading the link you gave. thank you. just wanted to know why it did not give any output inside second function..without using global keyword. – suryavansh Apr 05 '19 at 20:08
  • @miken sir...kindly go through my words again....i found your shared link kind of knowledgeable as i can understand the concept of scope. i worked really hard on scope coz i am coming from JavaScript..:)....and the link you provided did not really explained why my second and third function echo different. but i got it earlier in my accepted answer. thank you. – suryavansh Apr 06 '19 at 14:40

1 Answers1

2

myTest1() and myTest2() work the same because the declaration global $x, $y; means that the variables $x and $y inside the function refer to the global variables, which are the same as $GLOBALS['x'] and $GLOBALS['y'].

But myTest2() has no global declaration. When it assigns to $GLOBALS['y'], this updates the global variable $y, but not the local variable with the same name. Then it echoes $y, not $GLOBALS['y']. Since the local variable $y hasn't been assigned, it prints nothing.

If you enable error_reporting(E_ALL);, you'll see a warning from myTest2():

Notice: Undefined variable: y in filename.php on line 20

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you sir. the error E_ALL i fixed. and now what i understood is that.. that... in FIRST function....by using global keyword..i am literally creating new local variables for the first function inside it locally using the values from global variables ..right? and then echoed it out. . . then, in SECOND function..." how do i speak this code out in english - $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];" . . am i not creating new local scoped variables inside the second function by writing this -> "$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];" as it said on the site? – suryavansh Apr 05 '19 at 20:16
  • When you use `global $x, $y` you do *not* make new local variables. The function uses the global scope for those variables. – Barmar Apr 05 '19 at 20:30
  • sir. sorry to msg this here like this. but i feel you can help me. for me its serious. https://askubuntu.com/questions/1135717/ubuntu-18-04-keyboard-and-mouse-not-working-at-login-screen – suryavansh Apr 21 '19 at 08:45
  • I don't know anything about configuring Ubuntu. – Barmar Apr 21 '19 at 08:46