如果可以的话,我会进一步详细说明,但我遇到的主要问题是我无法访问函数之外的变量。我已尝试过以下两种方法。
以下是我的来源:
function register($rand, $code) {
global $rand, $access_token;
if ($code == $access_token)
$rand = rand('100000','1000000');
}
echo $rand;
答案 0 :(得分:1)
可能对你有所帮助
function register($rand, $code) {
global $rand, $access_token;
if ($code == $access_token) {
$rand = rand('100000','1000000');
}
return $rand;
}
echo寄存器($ rand,$ code);
答案 1 :(得分:0)
function register($rand, $code) {
if ($code == $access_token)
$rand = rand('100000','1000000');
}
然后先调用函数
register('1','2');
echo $rand;
答案 2 :(得分:0)
请考虑使用参数而不是使用$ GLOBALS或使用全局$ var,因为它建议[看看这些问题:one& two]
function register($rand, $code, $access_token) {
if ($code == $access_token){
$rand = rand('100000','1000000');
}
return $rand;
}
echo register($rand, $code, $access_token);
答案 3 :(得分:0)
如果你在访问$rand
变量之前调用函数,我想它应该运行正常。
function register($rand, $code) {
global $rand, $access_token;
if ($code == $access_token) {
$rand = rand('100000','1000000');
}
echo "\nInside Function = ".$rand;
echo $access_token;
}
register(100, '');
echo "\nOutside Function = ".$rand;
答案 4 :(得分:0)
你必须在设置$ rand之前调用该函数。函数内的全局$ rand在调用register()之前没有设置任何值。
<?php
function register($rand, $code) {
global $rand, $access_token;
if ($code == $access_token)
$rand = rand('100000','1000000');
}
register('x', 'y');
echo $rand;
?>
如果PHP在调用函数之前设置所有全局值,那么请考虑以下内容。 $ rand会被设置为什么?只有在调用其中一个函数时才有意义。
<?php
function a() {
global $rand = 1;
}
function b() {
global $rand = 2;
}