Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php没有';t在函数中获取变量,尽管有一个;“全球”;_Php_Function_Variables_Global - Fatal编程技术网

php没有';t在函数中获取变量,尽管有一个;“全球”;

php没有';t在函数中获取变量,尽管有一个;“全球”;,php,function,variables,global,Php,Function,Variables,Global,我有一个问题,我的PHP代码中有一个匿名函数,我必须用两个变量执行str\u replace,$user\u id和$first\u name。当我调用$user\u id和$first\u name时,它们是空的,但是如果我在函数之外使用它,它们就会工作。我用“全球”。“$database”有效 如果我这样做 echo$user\u id 我得到了用户名,与第一个用户名相同 修正了。多亏了里格斯的愚蠢。现在代码是这样的: 多亏了里格斯的愚蠢。这就是我问题的解决方案: $getTrans=函数(

我有一个问题,我的PHP代码中有一个匿名函数,我必须用两个变量执行
str\u replace
$user\u id
$first\u name
。当我调用
$user\u id
$first\u name
时,它们是空的,但是如果我在函数之外使用它,它们就会工作。我用“全球”。“$database”有效

如果我这样做

echo$user\u id

我得到了用户名,与第一个用户名相同

修正了。多亏了里格斯的愚蠢。现在代码是这样的:


多亏了里格斯的愚蠢。这就是我问题的解决方案:

$getTrans=函数($lang,$identifier)使用(&$message,&$username, &$user\u id和$first\u name)


您确定这些变量也在函数外部声明为全局变量吗?是的。你想看代码吗?谢谢你,真愚蠢。行得通,不客气。一个由你自己解决的问题总是更有意义:)和令人难忘
$getTrans = function($lang, $identifier)
{
    global $database, $username, $user_id, $first_name;
    $mammt = $database->prepare("SELECT * FROM translations WHERE language=? AND code=?");
    $mammt->execute([$lang, $identifier]);
    $trans = $mammt->fetch(PDO::FETCH_ASSOC)['translation'];
    if($trans == NULL){
        $mammt->execute(['italian', $identifier]);
        $trans = $mammt->fetch(PDO::FETCH_ASSOC)['translation'];
    }
    return str_replace(['\n', "{MENTION}"], ["\n", "<a href='tg://user?id=$user_id'>$first_name</a>"], $trans);
};
if (isset($update['message']['from_id'])){
    $user_id = $update['message']['from_id'];
}
$getTrans = function($lang, $identifier) use (&$message, &$username, &$user_id, &$first_name)