WEBVTT

00:00.440 --> 00:08.930
So in the previous video, I've mentioned the concept of references, and also we had to use a variable

00:08.930 --> 00:14.600
from outside the function to even be able to read the value of that variable.

00:14.600 --> 00:23.540
And this introduces two separate concepts the variable scope and the references.

00:23.630 --> 00:27.050
So in this video we're gonna talk about references.

00:27.050 --> 00:34.310
So I've kept those two topics for later on purpose because they might be complex.

00:34.310 --> 00:37.340
And we had to explain functions first.

00:37.340 --> 00:40.370
So now's the time to understand both.

00:40.400 --> 00:42.590
Let's start with references.

00:42.590 --> 00:49.400
We're gonna have a theoretical short explanation first and then we'll jump to the code examples.

00:50.240 --> 00:52.130
So let's use an example.

00:52.160 --> 00:57.290
Imagine you have this person variable to which you assign a string John.

00:57.470 --> 00:58.970
This is simple.

00:59.180 --> 01:05.960
And next you'd like the client variable that just be the same as the person.

01:05.960 --> 01:12.950
But in this case, we don't associate the value of the person variable to the client variable.

01:12.950 --> 01:17.870
Instead, this end sign creates a reference.

01:17.870 --> 01:24.860
So what reference means is that client is now an alliance to the person variable.

01:24.860 --> 01:34.100
And if you read the value of client or you set it, you are actually modifying the value of person variable.

01:34.280 --> 01:37.550
And those two are now connected.

01:37.550 --> 01:45.860
And that's because the client variable is essentially pointing to the same space in your computer memory

01:45.860 --> 01:50.330
or the server memory, which person variable does.

01:50.330 --> 01:54.140
So you are accessing the same space.

01:54.140 --> 01:58.430
So when you read the value of both variables they return.

01:58.430 --> 01:59.240
John.

01:59.480 --> 02:05.850
But what's more important when you change the value of either person or client.

02:05.880 --> 02:13.560
Both will change if I change the value of person by reassigning it to, let's say, Bobby.

02:13.590 --> 02:17.130
Then when I read client, it will return Bobby.

02:17.190 --> 02:25.710
If, on the other hand, I'm gonna change the client value to Richard, then if I read the person variable,

02:25.710 --> 02:27.390
it will return.

02:27.390 --> 02:28.500
Richard.

02:28.530 --> 02:33.420
So they are now both pointing to exactly the same space.

02:33.450 --> 02:36.150
And now let's see some examples.

02:38.790 --> 02:43.710
So in the main folder this time let's create a refs file.

02:43.740 --> 02:45.630
Refs peachpie is fine.

02:45.960 --> 02:48.240
And a PHP tag.

02:48.240 --> 02:51.360
And well let's use this example.

02:51.360 --> 02:55.740
So we had a person and this was John.

02:56.430 --> 02:59.400
Now let's create a client variable.

02:59.400 --> 03:02.730
And that's a reference to person.

03:06.090 --> 03:12.230
Now let's vardump the value of both the person and the client.

03:14.060 --> 03:18.890
Now I'm going to change the value of client to Robert.

03:19.730 --> 03:22.520
And let's also vardump both.

03:22.520 --> 03:30.890
And then let's change the value of person to Harry and Vardump both.

03:30.980 --> 03:32.570
Now let's run this.

03:32.570 --> 03:34.310
So we do PHP refs.

03:34.550 --> 03:35.000
PHP.

03:36.200 --> 03:42.140
So we are starting with both pointing to the value of John by creating this reference.

03:42.140 --> 03:44.390
And then you see the pattern.

03:44.420 --> 03:46.490
I've changed the client.

03:46.490 --> 03:51.590
So the one pointing to the original value and both show Robert.

03:52.400 --> 04:00.710
If I change the original person variable, the client is also pointing to the new value because they

04:00.710 --> 04:04.880
are pointing to the exactly same space in memory.

04:04.880 --> 04:08.030
So they have to have the same value.

04:10.300 --> 04:16.720
Now before you jump into your code and pass everything by reference and use references everywhere,

04:16.720 --> 04:25.990
I'd like to warn you not to do that, and just don't try to use the references for optimizations, because

04:26.200 --> 04:34.660
PHP is already pretty optimized, and using references is actually more dangerous because you can accidentally

04:34.660 --> 04:36.580
modify the value.

04:36.610 --> 04:44.860
So before you jump to any kind of optimizations, it's essential to first test your ideas.

04:44.860 --> 04:51.430
Just see how much faster your code is or how much less memory it's using.

04:51.460 --> 04:55.930
So you should never default to using references.

04:55.930 --> 05:03.310
Thinking about potential optimizations because you might be surprised that sometimes or even often the

05:03.310 --> 05:11.080
code without references, which are some additional overhead, might be faster and more memory efficient.
