How to use global variable in PHP

Variables that declare outside the function are global variables. Sometimes we need to call or use those variables in the function and without sending in the variable to the function, we can do as below

            <?php
            $a = "test"; //this is the global variable

            function printTest(){ //function for print out something
                global $a; //refer to global variable $a

                echo("this is ".$a);

                $a = "another test"; //change value of $a
            }

            printTest(); //call the function

            echo("<br />and this is ".$a); //print out after function
            ?>
            

Output:

this is test
and this is another test

Good! :-)