如何在 PHP 中的函数中本地使用全局数组中的值?

How can I use a value from a global array locally in a function in PHP?

感谢您阅读本文。我的问题是我无法从函数访问我的数组内部。例如:

$users = ["admin" => "admin","Alejandro" => "8345245"];
$userName = "Alejandro"
$UserPass = "8345245";
function checkUser(){
if( $users[$userName] == $userPass){
    return "The password is good";
}

这是我的问题,我知道在函数中使用全局变量我可以使用 GLOBALS 但如果我在数组中使用 $GLOBALS["users[$GLOBALS["userName"]"] 它不起作用出色地。 非常感谢。

见; The global keyword。 您可以在函数的开头简单地使用 global $users; 。像这样:

$users = ["admin" => "admin","Alejandro" => "8345245"];
$userName = "Alejandro"
$UserPass = "8345245";

function checkUser() 
{
    global $users,$userName,$UserPass;
    if( $users[$userName] == $userPass) {
        return "The password is good";
    }
}

echo checkUser();

但这更有意义:

$users = ["admin" => "admin","Alejandro" => "8345245"];

function checkUser($userName,$UserPass) 
{
    global $users;
    if( $users[$userName] == $userPass) {
        return "The password is good";
    }
}

echo checkUser("Alejandro","8345245");