gpt4 book ai didi

php - undefined variable 'count'

转载 作者:行者123 更新时间:2023-12-02 08:57:05 24 4
gpt4 key购买 nike

我已经有一段时间没有使用 PHP 了,所以我有点困惑为什么我在标题中收到错误

$count =0;
User::chunk(200, function ($users) {
$count++;
error_log('------------ chunck: '.$count);
});

最佳答案

您必须使用use,如文档(http://php.net/manual/en/functions.anonymous.php)中所述:

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing.

代码:

$count =0;
User::chunk(200, function ($users) use($count) {
$count++;
error_log('------------ chunck: '.$count);
});

关于php - undefined variable 'count',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43308418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com