The easiest way (and bad way in larger PHP projects, as this can make things very messy very fast), is to either use $GLOBALS['myvar']
to set/get the variable value for $myvar in a global scope. Or you can use:
<?php
global $myvar;
//use the $myvar below...
Use that anywhere, including inside a function, or in a new file, to use $myvar in a global scope.
On a side note, there are better ways to make something accessible "everywhere" depending on context. It may be a little outside the scope of this global question, but just be aware there are other ways, for instance using the Singleton method (enforce single instance of a class used throughout the software, retrieved with a static class that returns the object, like MyClass::getInstance()
). For a simple 2/3 file script this probably doesn't matter as much (unless you need re-usable code), but for anything more complex, avoid using globals for everything. You will save a lot of headache, the code will be easier to maintain, and your peers won't snicker at you behind your back for using globals.