In programming, when we pass a variable into a function, the function can either modify the original variable or create a copy of it to work with. This leads to two main strategies: pass-by-reference and pass-by-value.
Pass-by-reference means that when we pass a variable into a function, the function works directly with the original variable. Any changes made to the variable inside the function affect the original variable.
On the other hand, Pass-by-value means that when we pass a variable into a function, the function makes a copy of the variable and works with the copy. Any changes made to the variable inside the function only affect the copy, not the original variable.
$a = 14; $incr = fn($a) => $a++; $incr($a); echo $a; // Outputs: 14
In this code,
$a is passed by value into the arrow function
$incr. The function
$incr increments the value of
$a, but this only affects the copy of
$a inside the function, not the original variable
$a. Hence, after calling
$incr($a), the value of
$a remains 14.
let a = 14; let incr = (a) => a++; incr(a); console.log(a); // Outputs: 14
a is passed by value into the arrow function
incr. The function
incr increments the value of
a, but this only affects the copy of
a inside the function, not the original variable
a. Hence, after calling
incr(a), the value of
a remains 14.