简体   繁体   English

出现错误:将变量传递到 Octobercms 闭包 function 时出现“未定义变量:var”

[英]Getting Error: “Undefined Variable : var” when passing a variable into a Octobercms closure function

I have looking a way to pass a variable inside a http client method.我正在寻找一种在 http 客户端方法中传递变量的方法。 For example:例如:

$response_send_email = Http::get('some url',function($http){
   $http->data(['id'=>$var]);
});

I have tried to pass the variable, but it always showed an error "Undefined Variable: var" .我试图传递变量,但它总是显示错误"Undefined Variable: var" Before that, I have tried another way like this and it works:在此之前,我尝试过另一种这样的方法,它可以工作:

$response_send_email = Http::get('some url',function($http){
       $http->data(['id'=>1]);
});

But I won't do that way because I will pass a random id.但我不会那样做,因为我会传递一个随机 ID。 Thank You!谢谢你!

you need to use use keyword use ($var)你需要使用use关键字use ($var)

$var = 1; // <- outside scope variable
$response_send_email = Http::get('some url',function($http) use ($var) {
   $http->data(['id' => $var]);
});

if any doubt please comment.如有任何疑问,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM