So now we're going to talk about Variable Scope. What is Variable Scope? Well, we have code that's in a function, variables inside the function. This basically, whatever we do is not supposed to affect the outside world. We talked about call by value where you pass a copy in, and then we have call by reference which is like there's this little tiny doorway to one thing out here that you can mess with through the little doorway. So that's what scope is. Scope is, we're going to hide stuff or we're going to let this leak out. But in general, what it really means is the things inside of a function do not leak out. There's no mixing, you can choose the variable names the same and there's no mixing. And so in this case we have this function, the normal case is it's fully isolated. We're not passing a parameter in here. So this $val is not really the same as this $val. There is a $val out here and inside here there's also a $val. These are not the same thing, they're not the same thing. This one is living inside it. It's namespaced, it's the $val within the function, Tryzap is the way to think about this and this is the $val that is in the global context. This is the global. This is a big script. The outer $val and then the inner $val. So we make a wall, wall, big wall. Nobody sneaks through the wall. And of course superglobals. Right. $_GET sneaks through the wall. If it's the $_GET or $_POST or $_SESSION which we will learn about later, they're called superglobals, but let's ignore that for the moment. Of course, once I draw you a picture of a wall that cannot be penetrated, I say, oh here's how you get out. So how do we escape the wall? So the way you escape the wall is using global. Global says, open a door and go find me outside, making, if necessary, that variable name $val. So out here we've got this $val 10, we stick 10 in, we say global, so now $val is really just an alias to that $val. The names got to be the same because it's matching based on the name. Go find me the variable name $val outside of me. Go to the global one. And now if I set it to 100, it's a 100, come back prone out 100 because it pulls this out. Really simple, break the little wall, the wall is really bit solid except for this one doorway that we put in and we sneak out. Now, be careful. Last thing you want to do is a lot of global variables especially with funky names, especially a variable named $val or a $i variable because you might be in a loop on the outside and $I might be going up, a new global $I and you can blow the loop up. You can mess it up, you can mess up code that you're supposed to be serving as a function. Yet you mess it up because you're messing with the variables that the outer code is using. So this is supposed to be very rarely done. So if I create a global variable that I tend to avoid it at all costs, if you can pass it in by a parameter, passing it back by return value. These are like the two happiest things and they make every programmer happy if you can make a function that takes in copy by value, parameters and passes the return value, that's like a pure function that has no side effects. You can see, read about. So if you have a no side effects function that's like ideal, that everything inside the function, the contract with the outside world is in goes the parameters but you can't change them and out goes the return, nice. Passing a variable by reference, that's okay but you know if you can avoid that this is the best. These two things are ideal. Passing the references is okay and PHP is more okay than other languages. So global variables is like your last resort, like I got to have a global variable and then what I suggest and what most people do is have use long long names because you don't want to inadvertently collide with somebody that just happens to be using your code. Now maybe you're communicating and you're saying, Hey, I'll set this global variable in my functions all done and you can grab it. So these are example variables that I've created for us. Actually the code that does your auto graders. LastOAuthBodyBaseString. Sometimes I make them really long with camel case, sometimes I like them really long with uppercase snake case, camel cases is the first word is capitalized. Snake cases is this where it all stays lowercase. In this case is upper case snake case but I use these long names with nice unique prefixes because hopefully it also emphasizes to me that I'm not supposed to use them, and it reduces the chance of collision because there is only one namespace for global variables. Another thing interesting about PHP is that PHP evolves over time and I mentioned PHP 1, 2, 3, 4, 5, 7, remember 6 doesn't exist. There's a lot that's changed over the years, like object orientation came in five and now coalescing came in 7. So there are things that only exist in later versions of PHP. If you want to write code that runs on these earlier versions you have to come up with something that deals with that. If function_exists(array_combine) and array_combine is something that came, this returns a true false. You pass a string with the name of the function and you can ask. And it allows your code to evolve and cope with different environments and sometimes you don't have everything install the PHP and so you can check to see if your particular instance of PHP has a function that is an optional thing so there is core stuff and optional things. If you've installed one of our mamp per xampp distributions you'll find it a lot of the optional stuff. And so you don't worry so much about this, although your move in a production be like, oh where do all these things go? I'm like, we didn't put those things in, so you'll have to find ways to work around. So function_exists() is a way for you to write code that's smart enough to declare enough. Sometimes what you'd say is like if recombined doesn't exist in the version you can actually go to Stack Overflow and say, Could you give me a pure PHP implementation of array_combine. So the thing you'll do is you'll check to see if array_combine doesn't exist in your PHP, and if it does you'll actually put a function that says it does not exist right. You'd actually define the function here. So that's a way. You can't define the function if it's already there. And that would cause an error. So you just say, if this function is not already here then add this function. Which is a way for you to be backwards compatible or sometimes forwards compatible, sometimes they take things out and then you can say, oh wow it doesn't exist, so I'm going to write this or I'm going to check set of variables so I do it differently, etc. And one of the things that they're very good and very consistent about in the PHP documentation is telling you which versions this PHP works in. So this array_key_exists was clearly introduced in PHP 4.0.7. So it's there and in PHP 5 and PHP 7. Remember, 6 is the magical release that doesn't exist for PHP. And so you want to use this, you can check to see if it doesn't exist. And if not, you can write your own little simple version of it and you can probably say, Stack Overflow array, Give me a pure PHP version of array_key_exist for earlier than PHP 4. Now, PHP 4 is pretty darn old. So you probably aren't going to run into PHP 4, but you will run into for some time. Even though PHP 7 has been out for a while and it's awesome, you'll still run into PHP 5 systems for a very very long time. Now one of the things that I mentioned is that PHP is like super configurable and has lots of capabilities that can be optionally plugged in and sometimes you actually have to figure out what is in your PHP installation. Call the system administrator and say, hey, what's going on. But it turns out PHP has a really cool built in way to dump this stuff for you. So you write one file, I often call this file info.php. And you put the exact three lines in it, and this is a function that's built in PHP that dumps the configuration of a particular, so you could run this on your laptop, you could run this on your server, you could run it somewhere else, and you can figure out what version it is, how it's configured, where it stores its log files, et cetera et cetera et cetera. And we will be looking at this. If using MAMP or XAMPP there's usually a button called PHP info that just shows you this stuff, which is quite nice, because it's important to figure out what's in there, because there's lots of things that are plugged in. This is why I recommend XAMPP and MAMP because they put a ton of stuff into them, so you don't have to go like, oh I'm missing something. So that's nice because they choose all that stuff. And so this is what the output of that looks like, and it's page upon page of configuration dump, of where this is at, what the configuration setting is, what's been compiled into PHP because it's so darn flexible. So next we're going to talk about how you take an application, the web pages, and move them into multiple files for common elements that occur on many pages.