I would recommend passing in the variable, but if you want to use a global one, you can do this
function test2() {
global $foo;
...
}
at the top of your function and set the value somewhere else, or go with your second idea - but you need to specify a default value in the function parameter to make it optional.
function test2($foo='') {
if (empty($foo)) $foo = $_SERVER['PATH'];
echo $foo;
}
Another way to work with variables from outside your function is to pass them in by reference. This passes in a reference to the original variable, not a copy, so any changes you make inside the function will affect the original variable value outside of the scope of the function as well.
$foo = 'bar';
function test2(&$foo) {
echo $foo; // this will output "bar"
}