Re: Passing parameters by reference
The passing parameters by reference is not complicated either. Let's take a simple example and then explain how it works:
Code:
<? php
ajouter_cinq function ($ number)
(
+ $ number = 5; / / equivalent of $ number = $ number + 5
return $ number;
)
$ mon_entier = 15;
ajouter_cinq echo ($ mon_entier); / / display 20
echo $ mon_entier / / display 15
>
Here we do what is called a parameter passing by recopy. Do not take it too the head with these words, it is a little general;) In fact it means that the variable that we will move into the function ($ mon_entier here) will be copied in a another variable called $ number (which will take the same value). The function then uses this variable $ number for his treatment, not the $ mon_entier. That is why when displaying the contents of the variable $ mon_entier after calling the function, we get always 15.
Now with the version passed by reference:
Code:
<? php
ajouter_cinq function ($ number)
(
+ $ number = 5; / / equivalent of $ number = $ number + 5
return $ number;
)
$ mon_entier = 15;
echo ajouter_cinq (& $ mon_entier); / / display 20
echo $ mon_entier / / display 20
>
The advantage of this type of operation is that you work directly on the variable Originally, there was no recopy and therefore performance can be improved. You also have more need to return a value. Consider this example which is exactly the same as the previous one:
Code:
<? php
ajouter_cinq function ($ number)
(
+ $ number = 5; / / equivalent of $ number = $ number + 5
)
$ mon_entier = 15;
ajouter_cinq (& $ mon_entier);
echo $ mon_entier / / display 20
>