So next up let's cover the references in terms of functions.
So with functions the default behavior is always to pass the value by value.
This means that like in this example I would like to pass the some var variable to my function and by
default it's passed by value.
This means that a copy of this variable is created inside the function and the value is copied.
So if I perform some operations on by copy argument, I am modifying my local copy of that variable
inside the function.
The original variable is unaffected.
Now if you pass by reference so you add this end sign before the argument.
It's different.
Then as you see, the color is the same, which means we are working on exactly the same variable.
So other var passed to a function and then multiply it like this will modify this original variable
because by ref in this case is just a reference, just an alias.
Now let's define the value that we'd like to modify.
Let's just call it original, and let's just vardump the value of original.
After we call this function, this will be double value.
And I'm passing this original variable.
Let's just run that.
That's by ref php.
So the value of this original variable is five, which means it was unchanged because this function
creates a copy of this variable that only exists in the context of this function.
Thus, even modifying the argument like that does not really Modify the value that was passed when you
call this function.
Now it's another story when you pass by reference by adding this end sign.
Let me run that again.
Now we see that var dumping this variable named original returns ten, which means that it was modified
by running this function.
This is what passing by reference is.
Now if you are wondering what's the use case of passing by reference?
Again, I'd like to stress to not try to use this as a performance optimization unless you have reasons
first, and you can only have reasons if you have tested the code, and you have the raw data showing
you that the code is much faster or much more memory efficient.
So such experiments make sense.
If you have some heavy usage.
Let's say you've got a lot of visitors to your application or website.
You are doing some expensive calculations and you've done the experiments and see the data.
Otherwise, you might be surprised that using references might be slower or use more memory.
Again, don't use this for performance reasons.
Now, one of the use cases of references in functions or passing by reference might be that it basically
lets you actually modify a couple of values.
Otherwise, with functions you can only return one value.
And that's the case where you can pass, let's say three five parameters and modify them all instead
of just being able to return one value.
Now also keep in mind that just because something can be done doesn't mean that it should be done.
So I personally am not a big fan of this pattern of trying to modify multiple values by passing them
by reference.
I don't think that this is very intuitive and I would do this otherwise.
I would either use arrays to return multiple values or maybe objects.
Now, we haven't talked about objects yet, but just so you know that this is possible.
You can pass values by reference, but typically you just need to have a very good reason to explain
that.
And you just need to be aware and remember that PHP is optimizing a lot.
So if you haven't run any measurements, then probably you shouldn't be trying to optimize and just
stick to the default PHP behavior.
And pass by value.