Hello, in this video we are going to cover variable scope in, you know within the function round. So what is variable scope. So what it basically means is if you create a variable within the function, so not just using it, but that's actually when it's been created. So, first use, you can't use the outside of the variable unless you basically really clear so what will happen so if I create a variable here, so this is outside, so let's say outside the wall, and I'm going to make this equal to Hi. I'm going to literally use it here. So dollar I saw ball and then I'm going to run the function and this one variable to do to run it inside here.
We need to put global and then put down Let me call. Okay, so yeah, this is a no thing. If you have a variable outside, you can use it within a function, but you need to put global because this is a global variable in other programming languages, they just let you use it in PHP just need to, you know, use a keyword global. Okay, so that just means the function has access to it, which is fine. And you know, obviously we can use it down here as well. So let me what I'm going to do is put a new nine tag here.
And I'm literally going to use the outside bar here as well just so you can see I'm allowed to use it here. And we literally should just get high twice on two separate lines as we do. Watch what happens if I create a variable in here for dollar inside and also on site. So now, we could put it here and I'm gonna put some quotation marks like a colon there and then and then I'm gonna put the variable so dollar inside we run that fine we get Hi I'm inside wife only this variable x here. So we let's just try putting this as you can see we get an error undefined variable inside var on line 14, because the only place that we've used it and you know assign values to to it is inside the function it has what is called a local scope, which is essentially restricted to the start and end of these curly braces.
If for example, we will To do this, for example, on slide five, I'm doing my wireframe. That will find simply for the reason that I've assigned a value to it. So it's, it's a variable that is on the outside now. But if you are creating a variable, and it's just inside the function, then it has what is called local scope and can only be used within that function or where the curly braces are anywhere, there's curly braces, this really applies. And if it's outside, you can use it obviously outside inside if you use the keyword global. And inside variables, or local variables are great, because you might be thinking this is make everything global, make things easier, because we only use it, we can use it.
If you're working on a team, for example. And you have, you know, you're part of the application that you're programming. And you don't want the other developer in a mean way, but you don't want them to have access to certain, you know, aspects of the code. Now certain variables that you don't want them to accidentally manipulate the values? Maybe they're crucial critical variables, then you can make them local. So it reduces the scope plus this is removed from memory as soon as it hits the curly brace was lost.
And that's it. Any questions, feel free to pop me a message and as usual, I look forward to seeing you in the next video.