‫In this lesson we're going to talk about scope.
‫And scope is really important in all programming languages.
‫And it refers to the visibility of variables and the content context of where they're defined and where
‫they're used.
‫So let me give you an example.
‫So in the if I just do PHP right we have we're not inside of a function or anything like that.
‫This is what we call the global scope okay.
‫We're not inside of a function.
‫It's the highest level that we can go.
‫So I can define a variable here like let's say name equals Alice.
‫And I can echo out that name.
‫And of course that's going to work because this name is defined in the global scope.
‫And it's being echoed in the global scope.
‫Now when we have a function like let's say function say hello.
‫And let's say from here I want to echo a string of hello space and then I'll concatenate on the name
‫to that.
‫So if I do that, of course I have to call it.
‫Let's get rid of this echo and let's call say hello.
‫I'm going to get an error.
‫It says undefined variable name.
‫So even though I defined name as Alice in the global scope in this function, this is the local scope
‫to this particular function.
‫And this might surprise you, especially if you're coming from a language like JavaScript, because
‫JavaScript has something called lexical scoping where you can access variables from the containing scope
‫automatically.
‫So the global scope contains this function's local scope.
‫And again in JavaScript I would be able to do this.
‫But in PHP we don't have that functionality.
‫If you want to use that name, there's a couple options.
‫You could pass it in as a parameter, right.
‫So if we say name.
‫And down here we pass in.
‫Name.
‫And we reload.
‫Then we get hello Alice.
‫So that's usually the way you would you would do it is and that's the way you should want to do it.
‫Because functions are made for encapsulation.
‫You should have everything you need in that function.
‫Anything from the outside should come in as an argument through through a parameter, as we talked about
‫in the last video.
‫Now, if you do want to use the global scoped variable and not want to pass it in for some reason,
‫you can do it right.
‫But you have to define it here as global.
‫So you want to say global.
‫And then the name of the function I'm sorry the name of the variable.
‫So now if I reload I still get hello Alice.
‫And I didn't pass anything in here.
‫I'm using this particular variable.
‫If I change it to Alice one reload then it's going to show me Alice one.
‫So you can use global variables in a in a local scope.
‫But you do have to you have to declare them as global.
‫Now what if we want to change this variable of name?
‫Let's see what happens then.
‫So if I do, name equals Bob right.
‫So I'm reassigning it to Bob and I reload we get hello Bob.
‫Now, if I come out and back into the global scope and I echo out, yeah, we'll just say echo name.
‫Now, the name that it gives me is Alice, right?
‫And it's before hello, Bob.
‫Because hello, Bob is from this function.
‫So we get Alice here.
‫But if I were to echo it out after the function.
‫Then I get Bob.
‫So we were actually able to change the global variables value from within here.
‫And it affects it out here in the in the global scope.
‫So that's something you want to look out for.
‫And that's why you really don't want to do this unless you have to.
‫You again you want to keep your functions encapsulated.
‫And any changes from the outside world or the outside scope should come in through parameters for the
‫most part.
‫Now, if we define a variable in a function like let's, let's.
‫Get rid of this stuff here and let's create another function and we'll say say.
‫Goodbye.
‫And let's define this time we'll just we'll do it.
‫We'll do an array called names that has Jack.
‫In jail.
‫Okay.
‫And then, of course, in here, if I wanted to echo, let's say names and I want the first value.
‫So name zero and then I call Say goodbye.
‫Actually, let's echo out.
‫Just so this makes sense.
‫Good bye and then concatenate that on.
‫All right.
‫So if I do that reload I get goodbye Jack.
‫Now, if I try to access names from outside here, whether it's before or after, let's say echo names.
‫Zero.
‫So if I do that and reload, you'll see undefined variable names.
‫This goodbye Jack is just coming from inside the function, so I'll just comment that out.
‫What I what I want you to pay attention to is me defining names here and trying to use it here.
‫Now you might think that, well, just move it below the function because the function is where this
‫is defined.
‫So let's try that.
‫We'll move it below it reload.
‫And we still get undefined variable names.
‫Because again this is the local scope and this is the global scope okay.
‫And there's rules to the different contexts.