PHP - 在foreach循环中的变量之前的和号 [英] PHP - Ampersand before the variable in foreach loop
本文介绍了PHP - 在foreach循环中的变量之前的和号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能存在重复:
引用 - 这个符号在PHP中的含义是什么?
我需要知道为什么我们在foreach循环中的变量之前使用&符号
$ foreach $ wishdets ($ wisharray)($ wishesarray as $ categories =>& $ categoriesarray){
}
}
foreach code>
谢谢
解决方案
例子会显示你的区别
$ array = array(1,2);
foreach($ array为$ value){
$ value ++;
}
print_r($ array); // 1,2,因为我们迭代了
$ b foreach($ array as& $ value){
$ value ++;
}
print_r($ array); // 2,3,因为我们迭代了对数组
的实际值的引用。查看PHP文档这里: http://pl.php.net/manual/en/ control-structures.foreach.php
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I need to know why we use ampersand before the variable in foreach loop
foreach ($wishdets as $wishes => &$wishesarray) {
foreach ($wishesarray as $categories => &$categoriesarray) {
}
}
Thanks
解决方案
This example will show you the difference
$array = array(1, 2);
foreach ($array as $value) {
$value++;
}
print_r($array); // 1, 2 because we iterated over copy of value
foreach ($array as &$value) {
$value++;
}
print_r($array); // 2, 3 because we iterated over references to actual values of array
Check out the PHP docs for this here: http://pl.php.net/manual/en/control-structures.foreach.php
这篇关于PHP - 在foreach循环中的变量之前的和号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文