我遇到了问题。
我有3个文件,它们是:
apikey.php
<?php
$apikey = 'xxxxxxxx';
?>
core.php中
<?php
include 'apikey.php';
/* some other stuff here */
?>
的login.php
<?php
include 'core.php';
function GetUserData($id) {
global $apikey;
/* some other stuff here */
return $apikey; // <- returning NULL
}
?>
因此,正如评论所说,$ apikey被返回为NULL。
但是在GetUserData函数之外使用它可以正常工作。
有什么想法吗?
答案 0 :(得分:1)
你需要给函数提供$ apikey
function GetUserData($id, $apikey) {
答案 1 :(得分:0)
您可以使用$ GLOBALS变量。
<?php
function GetUserData($id) {
...
return $GLOBALS['apiKey'];
}
?>
或者您也可以将其定义为常量而不是apikey.php
<?php
define('API_KEY', 'xxxxxx');
?>
答案 2 :(得分:0)
尝试在apikey.php
中声明全局,然后定义它。
global $apikey;
$apikey = 'xxxx';
然而,使用全球你需要的是什么? 考虑将其作为参数传递。