Forum Moderators: coopster
What I am trying to do is set a "API Key" that is used throughout the site. I have a "functions.php" file that is included on every page that I thought I could just set the key in, however it doesn't seem to work. At the moment I have:
$apikey= '123456789';
function writeWidget($widgetid)
$param = array(
'apikey' => $apikey,
'widgetid' => $widgetid
);
.....
Notice: Undefined variable: apikey
If I hardcode it in the functino it works perfectly, any ideas, I must be being stupid!
In PHP global variables must be declared global inside a function if they are going to be used in that function.
Alternatively you could refer to the $GLOBALS[] array in order to access your global variable. This is a superglobal and can therefore be accessed directly in any scope.
function writeWidget($widgetid) {
$param = array(
'apikey' => $GLOBALS['apikey'],
'widgetid' => $widgetid
);
} Or, you could define() a constant (if apikey is constant?) instead of using a global variable. Constants can also be accessed in any scope (but cannot obviously be changed)...
define('APIKEY','123456789');
function writeWidget($widgetid) {
$param = array(
'apikey' => APIKEY,
'widgetid' => $widgetid
);
} "By convention, constant identifiers are always uppercase." (Constants - PHP Manual [uk.php.net])