minte9
LearnRemember



Rererence

In PHP variables are NOT passed by reference (& needed)
 
/**
 * Variable are passed by value (not by reference)
 */

$a =  1; 
$x =  $a; 
$y =& $a; // Look Here
$a =  2;

var_dump($x == 1); // true
var_dump($x != 2); // true
var_dump($y == 2); // true

Array

Reference with foreach changes the array.
 
/**
 * Reference with foreach changes the array
 * Use it with care
 */

$A = [1, 2, 3, 4];

foreach ($A as &$val) { // Look Here
    $val *= 2;
}

var_dump($A == [2, 4, 6, 8]); // true
var_dump($A != [1, 2, 3, 4]); // true



  Last update: 451 days ago