Thrown by the reference operator in “f1” not changing the contents of $arr2

I am also thrown by the reference operator in “f1” not changing the contents of $arr2 by reference when “f1” is called from “f2”. Surely this is assignment by reference and a return statement isn’t needed.?

Why doesn’t $arr2 have it’s value changed? I just don’t get it.

Is there a requirement that when a function is called from within another function, the called function MUST have a return statement otherwise the result will always be NULL? Even if the called function is using assignment by reference on the array that’s passed in.

I’ve tried adding a return in “f1”, doesn’t make any difference to what echo shows.

Hi,

These are two distinct types of strategies in function usage: pass-by-reference, and pass-by-value.

Reference refers to the memory address of the actual parameter and therefore modifies said value.

Passing by value simply makes a copy of the parameter value. Therefore changing it has no effect on the original.

Further reading: language agnostic - What's the difference between passing by reference vs. passing by value? - Stack Overflow

Hi,

Thanks for replying. I totally understand the distinction between those two strategies.
I’m trying to get a better grip on what’s happening when the f2 function passes the value into f1.
Is it the case that at that point the reference to the original array is lost because the passed argument points to an internal variable scoped only to f2 and therefore f1 cannot assign a value by reference to the origin of the value - $arr2?

All the best,
Martin