Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've seen how the variable names inside the function and outside the function are unrelated, and we wrote a function to do addition. The variable's sum was only available inside the function. We couldn't use echo outside the function to retrieve that value, we had to use a return value instead. Having this division between the variables inside a function and outside a function, is known as variable scope. A variable isn't accessible all the time. It's only accessible within it's context. A variable created inside a function, is by default only accessible In the function we say that the function is the variable's scope.
In PHP there are two main scopes. There is the Global Scope and there is the Local Scope. Let's take a look at them so we can see the difference. Let's start by taking basic dot HTML, calling Save As on it. We're going to call this functions_scope. And I'm going to end it with php, Save it. Now, let's create ourselves a little example, here. Let's call this Functions: Scope. I'll start my php tags. And let's assign a value to bar equal to outside.
This is going to be in the global scope. Now let's write a function. Function, call it foo and inside that function that's once again set bar equal to, this time it'll be inside. That is going to be local scope, alright? So now down here let's echo 1 and catenate with the value of bar followed by br tags, there we go. And then we'll call the foo function.
(SOUND). And then, once again, we'll just echo back what bar is, this time, with the number 2 in front of it, so that we can really see the difference, alright? Clear what we're doing? We're looking at the value of bar. Then we're calling the foo function, which is setting the value of bar. And then we're echoing it again. Alright, let's try it. Let's see what we get. Le'ts go to Firefox, instead of multiple returns, it's going to be functions_scope. Okay, they both returned outside to us. So this had no effect, what so ever, we did not set it to inside and that's because, this bar here is not the same as this bar here. Even though they have the same name, they have different scope. So it's actually a different variable as far as PHP is concerned. We can even test that further by going inside the function, and let's put a new line there, let's say if is set bar, then, and let's just do our echo here. We'll just paste it up here, but this time, we'll do it from inside foo, all right? So if it's set, then let's echo it inside the function.
Let's take a look at what that looks like, see? Nothing came out, because it wasn't set. Now, it does seem to matter if we provide bar as an argument to foo, right? We've also gotta have it down here. Let's send bar in. Now, this is the bar that was set here, all right? That's what we're passing in. Think about what's going to happen and then let's take a peak. Notice that it's outside all the way through. It still didn't change it, right? Because it got in here and it was local scope.
When it exits the function, well, this bar right here, it's the same bar that we were working with before. The change didn't stick, because these changes were not to this variable, they were to the local variable, the local scope. Now there is a way around this, though. There is a way for us to bring in the bar that's outside and to use it inside the function. If we go back to our house metaphor, it's like opening a window so that we do have access to it. And the way that we do that is by using the keyword global, followed by bar. Now, we'll take this away.
We no longer have the argument anymore. So let's go ahead and leave this all in here. And let's now, we've declared it as being global. Let's take a look at what happens. There we are, did you see it change? So now, it's outside, when we echo it the first time, we call the foo function, which brings in from the global scope, bar. Then, we have access to it, so now this bar, it's checking to see if it's set, that's the global one. That's the one up here that was outside, so that what it outputs for foo. Then we set that same one equal to inside.
When we leave the function we're back in the global scope but our global scoped variable is what changed. So that's why we get our third result as being inside. So global allows you to bring in globally scoped variable for use inside a local scope. Now use this with caution because you are effecting that global scope variable, from somewhere deep inside a function. That's not immediately obvious to you when you call foo that that's what you're doing. That you might be changing some of these values, right? It's not very transparent and easy to see.
You might be better off passing in a value than changing the value and catching the return value that comes out of it. That would be a lot clearer. And definitely don't use globals as a crutch or a way to keep from writing good code. In my opinion you probably won't use more than three, four, or five variables that get declared as globals in most projects. It really is just some special cases of things that you need to bring in. Most of the time you're going to want to use your arguments and return values instead.
Now even though you may never use that keyword global, what's most important is that you understand the difference between the two scopes. And how they either interact or don't interact.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97118 Viewers
61 Video lessons · 84417 Viewers
71 Video lessons · 68626 Viewers
56 Video lessons · 101113 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.