WEBVTT

00:00.620 --> 00:05.090
So next up let's cover the references in terms of functions.

00:05.330 --> 00:11.900
So with functions the default behavior is always to pass the value by value.

00:11.930 --> 00:20.840
This means that like in this example I would like to pass the some var variable to my function and by

00:20.840 --> 00:23.000
default it's passed by value.

00:23.030 --> 00:29.990
This means that a copy of this variable is created inside the function and the value is copied.

00:30.020 --> 00:39.350
So if I perform some operations on by copy argument, I am modifying my local copy of that variable

00:39.350 --> 00:41.180
inside the function.

00:41.180 --> 00:44.570
The original variable is unaffected.

00:45.320 --> 00:51.860
Now if you pass by reference so you add this end sign before the argument.

00:52.220 --> 00:53.630
It's different.

00:53.630 --> 00:59.300
Then as you see, the color is the same, which means we are working on exactly the same variable.

00:59.330 --> 01:10.750
So other var passed to a function and then multiply it like this will modify this original variable

01:10.780 --> 01:15.880
because by ref in this case is just a reference, just an alias.

01:18.190 --> 01:21.100
Now let's define the value that we'd like to modify.

01:21.100 --> 01:28.480
Let's just call it original, and let's just vardump the value of original.

01:28.990 --> 01:33.850
After we call this function, this will be double value.

01:33.850 --> 01:37.420
And I'm passing this original variable.

01:37.450 --> 01:38.830
Let's just run that.

01:38.860 --> 01:40.360
That's by ref php.

01:41.020 --> 01:48.970
So the value of this original variable is five, which means it was unchanged because this function

01:49.180 --> 01:55.060
creates a copy of this variable that only exists in the context of this function.

01:55.060 --> 02:03.080
Thus, even modifying the argument like that does not really Modify the value that was passed when you

02:03.110 --> 02:04.370
call this function.

02:04.400 --> 02:09.230
Now it's another story when you pass by reference by adding this end sign.

02:11.330 --> 02:13.040
Let me run that again.

02:13.340 --> 02:22.640
Now we see that var dumping this variable named original returns ten, which means that it was modified

02:22.640 --> 02:24.080
by running this function.

02:24.080 --> 02:26.900
This is what passing by reference is.

02:28.010 --> 02:33.590
Now if you are wondering what's the use case of passing by reference?

02:33.740 --> 02:42.050
Again, I'd like to stress to not try to use this as a performance optimization unless you have reasons

02:42.050 --> 02:49.520
first, and you can only have reasons if you have tested the code, and you have the raw data showing

02:49.520 --> 02:55.040
you that the code is much faster or much more memory efficient.

02:55.100 --> 02:58.610
So such experiments make sense.

02:58.610 --> 03:01.120
If you have some heavy usage.

03:01.120 --> 03:05.830
Let's say you've got a lot of visitors to your application or website.

03:05.860 --> 03:13.420
You are doing some expensive calculations and you've done the experiments and see the data.

03:13.450 --> 03:21.220
Otherwise, you might be surprised that using references might be slower or use more memory.

03:21.550 --> 03:25.300
Again, don't use this for performance reasons.

03:25.330 --> 03:34.600
Now, one of the use cases of references in functions or passing by reference might be that it basically

03:34.600 --> 03:37.390
lets you actually modify a couple of values.

03:37.780 --> 03:41.890
Otherwise, with functions you can only return one value.

03:42.100 --> 03:51.040
And that's the case where you can pass, let's say three five parameters and modify them all instead

03:51.040 --> 03:53.830
of just being able to return one value.

03:54.400 --> 04:01.610
Now also keep in mind that just because something can be done doesn't mean that it should be done.

04:01.670 --> 04:11.060
So I personally am not a big fan of this pattern of trying to modify multiple values by passing them

04:11.060 --> 04:12.350
by reference.

04:12.470 --> 04:19.670
I don't think that this is very intuitive and I would do this otherwise.

04:19.670 --> 04:26.090
I would either use arrays to return multiple values or maybe objects.

04:26.120 --> 04:32.150
Now, we haven't talked about objects yet, but just so you know that this is possible.

04:32.150 --> 04:39.170
You can pass values by reference, but typically you just need to have a very good reason to explain

04:39.170 --> 04:39.710
that.

04:40.010 --> 04:46.250
And you just need to be aware and remember that PHP is optimizing a lot.

04:46.250 --> 04:55.460
So if you haven't run any measurements, then probably you shouldn't be trying to optimize and just

04:55.460 --> 04:58.070
stick to the default PHP behavior.

04:58.070 --> 04:59.780
And pass by value.
