PHP – Ampersand before the variable in foreach loop [duplicate]

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)