PHP Tutorial (& MySQL) #14 - Variable Scope

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right my friends so now we know how to use functions I need to introduce this idea of variable scope because is really important and it probably can trip you up if you're not aware of it so then when we create variables they can have a certain scope it could be a global scope or it could be a local scope so I want to talk about local variables with local scope first of all then we'll move on to global scope variables okay so imagine we create this function called myfunc and inside we declare a variable and that's equal to 10 and then we echo that variable price so if we call this it's all going to work let's just try it out I'm gonna say my func like so and call it so save that and refresh over here and we see 10 so that all works now this variable right here when we create it inside a function like this this has local scope and by that I mean we can use it only inside this function if I try to echo the price outside the function I'm not going to be able to because I don't have access to it it's only available locally it has local scope I'm going to demo this save it and refresh it and we see down here in error one defined variable price so when we declare a variable inside a function it has local scope and we can't use it outside the function okay so let me just now comment this stuff out right here and we'll go down to this second example uncomment that this is a different function myfunc - it takes in a parameter a variable age and then we echo that age out now if I call this function I need to pass in an age argument so I'm going to call it my front two and I'm gonna pass in 25 and save that so this should work right if i refresh we should see 25 over here cool now this again even though we're passing it in from the outside here and assigning the variable there this still has only a local scope and we cannot access it again outside of the function so I'll demo that and refresh and we still get an error so remember this when we create a variable inside a function or inside here then it has local scope and it can only be used locally inside that function I hope that makes sense okay so global variables now so first of all outside of a function I'm gonna make this variable and I'm gonna call it name and set it equal to Mario okay so we have that variable now I'm going to create a function and I'm going to call this say hello and inside that function what I'm going to do is echo and hello and then we're going to use that name variable now we've declared this name variable outside of the function so do you think we can use it inside the function well let's save and find out I'm going to refresh over here and we don't see anything that's because we've not called the function so let's do that first of all say hello and then save and refresh and now we get an error okay undefined variable name so it's saying that this is undefined and that's because it's looking for a local variable by default inside the function so if we for example say well okay name in here is equal to Shawn then this would work it would say hello Shawn because it finds that local variable okay now what I want to do is not use this value I want to use this value so what we do is place the keyword global in front of name to turn this into a global variable and now we're going to access this variable which is declared outside of the function so we're using the global variable now and now it should work so if we save this and refresh that works and that's because we've declared that name when used inside this function here is going to be a global variable it's going to be this thing up here all right cool so now then what if inside this function I want to override this thing what's going to happen well let's have a look if I say now name is equal to Yoshi then run this function then we see Hello Yoshi now what do you think's happened has this been overridden outside here as well or just inside here locally well let's echo out the name down here again after the functions run after we've changed it inside the function let's see if it also changes outside of the function up here so save that and refresh and it does so when we say that this is a global variable and we change it inside the function it also changes the one outside of the function because we're now using this variable up here so whenever we reference name now it's using this variable up here not a local one and that's because of this statement okay then so let me just comment this junk out for a minute we don't need that I'm gonna do one more example so what I'm gonna do is come down here and make a new function and that this function is going to be called say by and it's going to take in a name parameter okay so inside I'm gonna say echo and then in a string double quotes bye and then the name okay now then unlike before this is expecting a parameter we didn't pass in a parameter here so when we're using this it's gonna look for this value so what if I now say okay well let's say bye and pass in this value okay so this variable so we're passing in that variable Mario into this thing over here then were echoing that out so we should see by mario shouldn't win so if we save that and refresh and we get an error and that's because we've missed off the semicolon right here okay never forget those save that refresh and we can see by Mario okay so that's worked so we've passed this value in to the function and this local variable now has been assigned the value of Mario then were outputting that but what if now we try to update the value of that name what if we say name is now equal to Wario well let's save that and see what happens if we refresh we see by Wario but now if we try to echo the name and save it is this gonna be updated will this be now Wario because we've changed it in here well let's give this a whirl if we refresh we see by barrio then Mario so the Mario right here where we echoed the name this has not been updated even though we update it right here so what's going on well remember when we use a parameter inside a function that is a local variable we talked about that at the top up here so now we're passing in a value of Mario but when it gets here we're declaring this new local variable called name and now this in here is a local variable not referring to this global one okay so when we use the global keyword over here and declare name inside the function then that is going to use the global variable and we can override it globally here but when we pass one in as an argument take it as a parameter as a local variable and try to override it it only overrides the name variable inside the function because this is now a local variable not a global one okay so it doesn't update that now if we want to pass this in by reference and be able to update this thing we can do that by adding an ampersand in front of this parameter so that is now saying okay well pass this variable in this here by reference okay so it's going to take in the name and it's going to change the outside here so it's similar to doing this now if we change inside it's going to change it globally as well because we're passing in the variable by reference that might be a little confusing and there's different ways to do this but once you get your head around it and you play with it a few times it's not that difficult to understand so if we save this now we should see that by worried out because we change in here and when we change in here because we're passing it by reference now it's going to update outside of the function as well and this should be Wario too so save that refresh over here and we see by WarioWare okay so that's just a little bit on variable scope again it's not always gonna get this difficult we're not going to be using this much but now hopefully you understand what this means if you ever see it in any PHP code or need to use it in future
Info
Channel: The Net Ninja
Views: 38,941
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, php scope, php variable scope, variable scope, global, php global, php global variable, php by reference, by reference
Id: YCw3z-yEiwQ
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Wed Feb 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.