Okay guys, now finally with references let's have an example with loops.
So this would be a for each loop ref.
So let me create such file.
So I just wanted without any preconditions.
Show you an example where we have a large data set generated but a large data set.
And we're going to see how can we access data by reference.
And we're going to see if there was any difference which version was faster.
It might also be useful for you to do such experiments.
So to this large array variable I'm going to assign the result of range function.
So range function allows us to generate a big array with some value.
Let's just use the default one.
And I'd like 1 million elements.
By the way, in PHP you can write numbers like this using underscore just for it to be more visually
readable.
This is still a valid number 1 million.
Now let's see how can we calculate the time of the execution.
So we can use the Microtime function and pass true to it.
So we're gonna get the time at specific points.
So this is before we run the code.
And then we're going to do the same after we run the code.
And we're gonna show the difference.
Okay.
So what are we about to do?
Well I would like to just double every element inside this large array.
Keep in mind it's 1 million elements.
And I just want the result.
So I just want the result to be stored someplace.
So if we use the for each array well we need a kind of an output array.
So let's define the out variable.
and by default this is an empty array.
Okay.
So now we can do for each and go over the large array where every element that we have.
Well we're going to add to the out array.
Now in PHP the simplest way to add a new element is using such syntax.
This essentially means add a new element at the end of this array.
And in this case that's just value times two.
And additionally I think that we can also keep track of the used memory.
And this can also be used.
This can also be done by using function.
This is memory get usage.
And also at the end we can get the end time by just running Microtime with the true parameter and also
the end memory.
Let's get it using memory.
Get usage.
All right.
And now let's output the results.
So first we need to display the time.
And let's concatenate the results of subtracting the end time or subtracting the start time from the
end time.
So this would be in seconds.
That's the first result.
And another result is the memory.
So time and memory.
So here let's round the result using the round function.
Since the result would be in bytes, let's also convert that to megabytes.
So first we do end memory minus start memory.
I think it's called start mem.
Let's rename that to memory.
All right.
And then we divide that by 1024 to convert to kilobytes.
And again 1024 converting to megabytes.
Quick reminder 1024 bytes makes one kilobyte 1024kB makes one megabyte.
Also, let's adjust this to be megabytes.
And now let's run that.
So this is for each dash ref.
And here are the results.
That's the time it took to process the array.
And that was the memory usage.
16MB.
Now let's see, what will we have with the references.
Now keep in mind that here we had to have two arrays.
So the large array and this outer array that just contained the results of those calculations.
Now let's modify this example to use references.
So with loops you can use references by adding the end sign before this variable.
So what this does with the loops is that instead of creating a copy of every element of the array that
we go over, we actually directly reference it.
So now.
If you do something like this, you are just gonna modify an entry inside this original array.
So this means with this example we don't need the out array as we are going to be modifying the original
value.
Okay, so let me run this example.
So let's quickly go over the results.
That's the time without references.
And this is the time with references.
So with references it's slower.
It took more time.
That's the memory without using references in loops.
And that's the memory used with references.
So it's almost double the memory.
That's why I'm saying this.
Be careful if you try to use references for optimizations.
So in this case using reference lets you directly modify the value in the original array, which wouldn't
be possible otherwise with the for each loop.
So that's the use case here.
Not any potential optimizations.