Here are three ways to do this that will work:
1) Create global var inside function:
<?php
// define function
function getGlobal(){
// establish a global variable
global $dogtrash;
// assign the global variable a value
$dogtrash = 'my global var';
// echo global var
echo $dogtrash;
}
// call function
getGlobal();
?>
Or the use of a CONSTANT, which are global in scope, and use that in your function:
<?php
// define a constant
define('DOGTRASH','my global var');
// define function
function getGlobal2(){
// echo Constant
echo DOGTRASH;
}
// call function
getGlobal2();
?>
Or, create the function to accept arguments, and you can set defaults, too:
<?php
// define function with an arguments
function getGlobal3($var = 'dogtrash', $val = 'my global var'){
//define global var
global $var;
// assing var a value
$var = $val;
// echo var
echo $var;
}
// call function
getGlobal3();
?>
... or....