PROGRAMMING

  MINTE9
REMEMBERS




Last update:   14-02-2022

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;

echo $x; // 1
echo $y; // 2

Array

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

$arr = array('1', '2', '3', '4');

foreach ($arr as &$val) {
    $val *= 2;
}

print_r($arr); // [2, 4, 6, 8]

Walk

Array walk is different when used with reference.
 
/**
 * Array walk with / without reference
 */

$A = [1.5, 2.6, 3.7];
array_walk($A, fn($v) => $v = floor($v));
print_r($A); 
    // 1.5, 2.6, 3.7
    
$A = [1.5, 2.6, 3.7];
array_walk($A, fn(&$v) => $v = floor($v));
print_r($A); 
    // 1, 2, 3

// map
$A = [1.5, 2.6, 3.7];
$A = array_map('floor', $A);
print_r($A); 
    // 1, 2, 3

Questions     github Basics
Integers
        A B C D E F
🔔
1/1