简体   繁体   English

严格标准错误

[英]Strict standards error

The function parse_users returns an array. function parse_users返回一个数组。

I am doing the following in another function:我在另一个 function 中执行以下操作:

return reset($this->parse_users($records));

But I get a Strict Standards: Only variables should be passed by reference in...但我得到了一个Strict Standards: Only variables should be passed by reference in...

Is it because I do a reset() on the function?是因为我在 function 上做了一个reset()吗?

Do I have to do it this way:我必须这样做吗:

$users = $this->parse_users($records);
return reset($users);

Or is something else?或者是别的什么?

That's it exactly.就是这样。 reset takes a reference to an array as a parameter, so it basically needs a real variable to reference -- even if it is a pass-by-reference value. reset将对数组的引用作为参数,因此它基本上需要一个真实变量来引用——即使它是一个按引用传递的值。

why didn't you try your你为什么不试试你的

$users = $this->parse_users($records);
return reset($users);

? ?

It's correct这是正确的

The one-line-solution uses an additional pair of brackets;单行解决方案使用一对额外的括号; this will turn the reference into a variable and omit the error:这会将引用转换为变量并省略错误:

return reset( ( $this->parse_users($records) ) );

From the PHP documentation for reset :PHP 文档重置

mixed reset ( array &$array )混合重置(数组 &$array )

reset() rewinds array's internal pointer to the first element and returns the value of the first array element. reset() 将数组的内部指针倒回到第一个元素并返回第一个数组元素的值。

The PHP reset() function accepts a reference to an array. PHP reset() function 接受对数组的引用 The strict warning is raised because you're directly passing the result of parse_users to reset, without a way to access that array in your other function.发出严格警告是因为您直接将 parse_users 的结果传递给重置,而无法访问其他 function 中的该数组。

If you're trying to return the full array (and not just the first value) after it's been reset, you should use:如果您在重置后尝试返回完整的数组(而不仅仅是第一个值),您应该使用:

$users = $this->parse_users($records);
reset($users);

return $users;

Alternatively, if you just want the first value from parse_users, you could just use:或者,如果您只想要来自 parse_users 的第一个值,您可以使用:

$users = $this->parse_users($records);
return $users[0];

The reset function is only needed when you're iterating over the array and want to make sure you start from the beginning.仅当您迭代数组并希望确保从头开始时,才需要重置 function。

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

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