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) { } }
It denotes that you pass $value by reference. If you change $value within the foreach loop, your array will be modified accordingly.
break ends execution of the current for , foreach , while , do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1 , only the immediate enclosing structure is broken out of.
The foreach loop is considered to be much better in performance to that of the generic for loop. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With