This section describes rules on how arguments can be passed from the calling code into the called function by value (the default) or by reference.
As shown in examples in previous sections, passing arguments to a function seems to be a simple job.
But it may cause confusion if you don't following the rules. PHP has the following rules on
passing arguments to function and subroutine procedures:
1. By default, a function argument is passed by value.
In this case, the argument variable contains a copy of the data provided
by the calling code.
2. But, a function argument can be passed by reference, if you define the argument variable as &$variable_name.
In this case, the argument variable contains a reference of the original variable in the calling code.
In another word, the argument variable is an alias of the original variable.
3. A pass-by-value argument variable is safer to use,
because it will be assigned with a copy of the data from the calling code at the time of call.
If the argument variable is re-assigned with a new data in the function, there will be no impact to the calling code.
4. A pass-by-reference argument variable offers an advantage and a risk.
The advantage is that it can be used as an output channel for the function to send data back to the calling code.
The risk is that if the argument variable is re-assigned with a new data in the function by a mistake, there will be some
negative impact on the calling code.
5. An argument variable can be defined with a default value. Arguments with default values must be positioned at the end of
the argument list.
6. If you want to use the default value of an argument variable, you can skip that argument in the function call operation.
7. Arguments passed in the function call be retrieved without using argument variables. You can use
func_num_args(), func_get_arg(), and func_get_args() functions to access arguments.
This feature can be used to support variable-length argument lists.
8. If an array is passed by value, a copy of the original array will be passed to the function.
If an array is passed by reference, the function and the calling code is sharing the same array.
9. If a reference of a variable is passed by value, a copy of the reference will be passed to the function.
But the copy and the reference are both referring to the same original variable. So passing a reference by value
is equivalent to passing a variable by reference.