0

I have heard using global variables is not good, however I am just trying to understand how the PHP language works. I am a beginner in the coding world.

Why can global variables be created within functions? Whether it is through the use of the global keyword or through a superglobal variable. I thought these two actions were used to access global variables in a function. I thought the only way you can create a global variable is to create it outside a function; in the global scope. I have looked at many different websites including w3schools.com and php.net

This is just some simple code I created to try and understand the way global variables work with functions:

<?php

function sample1() {
    global $a;
    echo $a = "this ";
}

sample1();

function sample2() {
    echo $GLOBALS['$b'] = "is ";
}

sample2();

function sample3() {
    global $c;
    $c = "an ";
}

sample3();
echo $c;

function sample4() {
    $GLOBALS['$d'] = "example ";
}

sample4();
echo $GLOBALS['$d'];

?>

This is the result of the code:

this is an example

All of the code works, but I don't understand how I created a global variable on any of these blocks of code? The global variables were not created outside of the functions. How can they be created inside of a function? What am I missing? Any response is appreciated - If possible, please keep the answer simple - I would like to discuss this further in the comment section, because I'm sure I will have follow up questions - Thank you

1 Answers1

0

Variables can be created in the global scope in the two ways you just did - there's nothing saying that a function can't create (or change) a variable in the global scope - WHEN you explicitly ask for it through $GLOBALS or the global keyword.

The issue is that your belief "I thought the only way you can create a global variable is to create it outside a function; in the global scope." is not an exact statement. When you're using $GLOBALS and global, you're referring to the global scope. You're introducing a reference to the global scope inside your function.

With global you're in effect linking the local reference to the global reference, while with $GLOBALS you're explicitly referencing the global scope (which internally inside PHP can be introduced to the local scope in the same way).

In that case you're explicitly saying "I want this variable to be available in the global scope, make it so!" and PHP does what you're asking it to. This behaviour differ between languages, but as you've discovered for PHP, it's allowed.

It's not something I would recommend using in any way - it makes your code very hard to follow and argue around, so consider it an esoteric detail.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • 1
    Thank you very much for your quick and thorough response!! I apologize for the delay of my comment, however, I wanted to try to comprehend what you were saying - I think I sort of understand now - so when you use a reference inside PHP, the value of two variables both become global and refer to the same value? If it's a reference, then how come both of the variables have the same name in both scopes? This website (https://www.elated.com/php-references/) says: A reference is simply a way to refer to the contents of a variable using a different name. Thank you again!! :D – spiderman0297 May 27 '20 at 04:26
  • There is nothing that "becomes global", but the local variable - i.e. what you're using inside the function, is a reference to the global variable - in effect; they're the same thing using two different names (even if the lexical name is the same inside the local scope and the global scope, they're effectively different - one is local, one is global - the "symbol" as it can be called is always local, but it refers to the same data as the global symbol / variable. In effect, it's the same data (variable) with two different names. – MatsLindh May 27 '20 at 07:47
  • 1
    Thank you very much for clearing this matter up for me!! I greatly appreciate the time you have given and the knowledge you have provided!! :D :D – spiderman0297 May 29 '20 at 04:30