我正在学习ZCPE,在StudyGuide中是下一句话:
passing an array in as a function argument, unless you pass-by-reference using the & operator, a copy is passed and the internal pointer is always set to the first position, making a call to reset() unnecessary
所以我做了一个测试:
function test($arr) {
echo current($arr);
}
$arr = array('a','b','c');
next($arr); next($arr);
test($arr);test()函数中的输出是c,这意味着数组参数显然是作为副本发送的,但内部指针克隆在与全局空间中的数组相同的位置。
文档是针对PHP5.3的,但我在PHP Manual中找不到这样的更改。
可能是个bug,但我不确定,因为我在手册中没有找到在将数组参数传递给函数时如何维护指针的信息。
任何有关此问题的信息都将不胜感激。
发布于 2014-11-01 23:51:45
您可以选择按引用传递参数或按复制传递参数,例如:
示例1
function fn($a) {
// some job
}
$a = 5;
fn(&$a); // i choose to pass variable a by referance, instead of my declaration示例2
function fn(&$a) {
// some job
}
fn($a); // will be passed by referance, whatever i do ...
fb(&$a) ; // also by referance : WARNING this has been removed from the new version of PHP !在官方的php文档中,提到了next()的参数会通过引用自动传递:
mixed next ( array &$array )所以它不是一个bug :)
希望这能有所帮助
https://stackoverflow.com/questions/26690420
复制相似问题