So let's talk about the scopes of variables in PHP.
So at the very top you've got the global scope.
And that's the scope of all the variables that were just declared outside the functions so directly
in PHP files.
And the global scope is shared across every single PHP file.
So if you use includes or you require some PHP files, they will share the same global scope.
The next scope is the local scope in functions.
So whenever you declare any variables inside functions, they are part of the local scope of that function
and this means they are not accessible outside this function.
So can't be accessed in a global scope or in other functions.
Same with function arguments.
So whatever you pass to a function, if that's passed by value, it's the local scope of that function,
and this variable is not accessible outside.
The last special case are the static variables inside functions.
So you can say that static variables are part of the function scope, but yet they are special.
So all the variables that you define in functions, they only exist for the duration of this function.
If the function stops and returns a value, the variables and arguments, they disappear.
They are no longer available.
And when you call the function again, you actually have new variables inside this function scope.
So the difference with the static variables in functions is that they actually preserve their state
between subsequent function calls.
So let's see examples of all of that.
And also let's see, how can we mix those scopes?
How can we access.
Global variables in functions and how the static variables work.
So it's time for examples.
Let's create the scope PHP file and add the starting tag.
Next up, let's add a global variable.
Let's call this superhero.
And this would be maybe Superman.
Next up we're going to create a function called reveal identity.
And here I'd like to say what's the real name of Superman.
So I use the superhero variable and I say real name is Clark Kent.
Let's call this function now and let's see if this would even work.
So we run PHP, scope, PHP, and we've got a warning that we've used undefined variable called superhero.
So that's the separation of scopes.
That's the global scope.
And inside this function between curly braces we've got the local scope of this function.
And we can't access directly at least the variables from the global scope.
So a way around that would be to just pass a parameter where you can just use name variable, or even
define the superhero argument and just pass the variable with the same name.
That's another thing that you can use the same variable names in different scopes.
So this superhero variable here, since we have.
The argument is something different than this one because those are separate scopes.
You can compare that to separate rooms.
You can have Peter in one room and another Peter in another room, and that's not the same Peter.
So that's one way, and I would say the preferred way to just pass arguments to functions.
But in PHP you also have the global statement which lets you, let's say import or just make available
some global variables inside functions.
And now this would work fine because we now access this original global variable.
Now let's mix that with the local scope.
So the variable I'm now creating is in the local scope of this reveal identity function.
And I'm going to move this part of the display displayed text to our local scope variable and just use
it inside the echo statement.
So the code works fine, because this is a variable that exists in the scope of this reveal identity,
and every single time this function is called a new variable with this value will be created.
But if for whatever reason you'd like to echo it again, you'd like to access a message outside the
function.
Here is what you get.
Warning as it's undefined.
Well, and this means it's not defined in the scope you are currently in, and you are currently in
at least in line 11.
Here you are in the global scope.
Okay.
So next up let's see the static variables.
Let me create another function for that.
And we're gonna count visitors.
Let's define the static variable.
You define the static variable by prepending it with the static keyword.
I'm using visitor count and this will equal zero.
Now what I'm doing here is I'm just going to increase this value by one.
So you probably remember this operator.
And we'll simply output something saying visitor and hash visitor count has arrived like that.
And now let's just run this function three times.
So remember how I previously said that the function variables.
So the functions that are defined in the local scope of the function, they only exist up until the
end of the function after the function is called and returns.
The values are destroyed.
They don't exist anymore, and when you call the function again, you've got a new variable inside this
function.
Let's see what happens with the static ones.
Let's rerun this script.
Well, I'm not sure this is clearly visible.
Probably you can read that easily, but let's run it again.
So what we see here instead is that we've got visitor one, visitor two, and visitor three.
Yet you can see we are using the same variable defined in the function local scope.
So this is what static does.
It preserves the state of a variable defined in the function.
So it might be a little bit counterintuitive at the first glance because you clearly see that the visitor
count is initialized with zero.
Yet when you call this function, it seems like this line isn't run on subsequent calls, only on the
first one.
And actually that's the case.
So here the first call just initializes this variable with zero.
And then this variable leaves even when this function stops.
As we've seen the subsequent returns just increase the value of visitor count that we had previously.
So the variable is shared across all calls of this function.
That's why it is a special scope.
So let's go back to this global keyword.
So this way of using variables from the global scope I think it existed in PHP version four or even
earlier.
And I generally don't think it's a good idea to depend on global variables in functions.
Let me give you some reasons.
So functions main use case is for them to be reusable.
If you depend on some global state this is tightly coupled with the global state which makes this function
less predictable and I would say hardly reusable.
Now it's also harder to test code.
That depends on some global state.
And another problem here might be some hidden side effects.
You also need to be aware of the unexpected side effects.
So imagine I am a new developer in a team and someone has created this function.
Maybe this function does not only read the variable of superhero, because this is not really a dangerous
example.
We are just reading some global value.
Maybe that's even fine, but maybe I'm gonna change it to spider man.
And that's possible.
You can do that.
And me, as a developer that have just joined the company, I need to reveal the identity of some superhero.
I'm calling this function and unexpected thing happens.
I actually change the value of global variable to Spider-Man, and I didn't know that.
So let's echo the value of superhero.
So as you see, that's Spider-Man.
So I've actually modified the global variable.
Now it's a made up example.
But I think you can see how this can be dangerous when you rely on some global data.
So in this case I would prefer that you use the function arguments and then you are clear about your
intentions.
And also when you create functions that are then used by other people, they don't know all the details
of those functions.
So the name of the function should be very descriptive.
And always remember not to put any side effects in a function.
The function should do only what its name suggests.
So if the name says reveal identity, don't change the identity, only reveal it.
Now, as with the static variables, well, I think they might have some better use cases.
So you can use that for the so-called memoization or caching, which just helps you keep some data if
the computation is expensive.
Let's imagine you've got an operation which takes a lot of CPU You power and you just want to keep the
results stored someplace between subsequent calls.
So you just save memory, you just save recalculations.
So you save basically the compute and memory, which essentially is saving money.
So maybe that's a sensible use case then.
Well counters like this, maybe that's a good use case.
If you have a specific example like this then there is something like a singleton like behaviour.
Well I would need to talk about design patterns for you to understand singletons.
Essentially, if you have a resource that should only be created once, that is for example a database
connection, you might use a static variable inside a function to keep the reference to this connection.
Then it might be something like this.
You have a function called get db where you declare that static DB is, well, nothing, and if DB is
null.
So if it wasn't initialized then you somehow create the connection.
So I'm just using made up code.
There is no connect function, just showing you the example and then you always return db.
So on all the subsequent calls to this get db function in your code, you are actually using the same
connection that was initialized once and is being kept on all subsequent calls.
This one is actually a very good use case of the static variables, if you would be using functions
for this purpose.
Okay, so let me comment this out because this actually wouldn't work.
That's just an example.
And by wouldn't work I mean there is no connect function.