So in the previous video, I've mentioned the concept of references, and also we had to use a variable
from outside the function to even be able to read the value of that variable.
And this introduces two separate concepts the variable scope and the references.
So in this video we're gonna talk about references.
So I've kept those two topics for later on purpose because they might be complex.
And we had to explain functions first.
So now's the time to understand both.
Let's start with references.
We're gonna have a theoretical short explanation first and then we'll jump to the code examples.
So let's use an example.
Imagine you have this person variable to which you assign a string John.
This is simple.
And next you'd like the client variable that just be the same as the person.
But in this case, we don't associate the value of the person variable to the client variable.
Instead, this end sign creates a reference.
So what reference means is that client is now an alliance to the person variable.
And if you read the value of client or you set it, you are actually modifying the value of person variable.
And those two are now connected.
And that's because the client variable is essentially pointing to the same space in your computer memory
or the server memory, which person variable does.
So you are accessing the same space.
So when you read the value of both variables they return.
John.
But what's more important when you change the value of either person or client.
Both will change if I change the value of person by reassigning it to, let's say, Bobby.
Then when I read client, it will return Bobby.
If, on the other hand, I'm gonna change the client value to Richard, then if I read the person variable,
it will return.
Richard.
So they are now both pointing to exactly the same space.
And now let's see some examples.
So in the main folder this time let's create a refs file.
Refs peachpie is fine.
And a PHP tag.
And well let's use this example.
So we had a person and this was John.
Now let's create a client variable.
And that's a reference to person.
Now let's vardump the value of both the person and the client.
Now I'm going to change the value of client to Robert.
And let's also vardump both.
And then let's change the value of person to Harry and Vardump both.
Now let's run this.
So we do PHP refs.
PHP.
So we are starting with both pointing to the value of John by creating this reference.
And then you see the pattern.
I've changed the client.
So the one pointing to the original value and both show Robert.
If I change the original person variable, the client is also pointing to the new value because they
are pointing to the exactly same space in memory.
So they have to have the same value.
Now before you jump into your code and pass everything by reference and use references everywhere,
I'd like to warn you not to do that, and just don't try to use the references for optimizations, because
PHP is already pretty optimized, and using references is actually more dangerous because you can accidentally
modify the value.
So before you jump to any kind of optimizations, it's essential to first test your ideas.
Just see how much faster your code is or how much less memory it's using.
So you should never default to using references.
Thinking about potential optimizations because you might be surprised that sometimes or even often the
code without references, which are some additional overhead, might be faster and more memory efficient.