So now, we're going to talk about writing functions, and how we can break functionality into multiple files. So, we need to learn about functions partly because we're going to write them, but mostly because we're going to use them. And PHP is a less object oriented programming language than some are, and so, we use functions all the time. But, we write our own functions as well and we're going to cover both of those things. A lot of times, programming, initial programming, tells you, you're supposed to write functions for everything. I'm not such a fan of writing functions, until you feel the need. A function is a great way to see this work that you're going to do over and over and maybe change a little tiny thing. That's when it's time to do a function. When you say, you know, I'm doing the same thing over and over and over. Don't repeat yourself. You make it work once, sometimes with parameters and then do that over and over again. Now, I find that I actually have an offline documentation viewer for PHP because I don't memorize all the functions and there are so many functions. So, for example, I never know what the replace function is for PHP. I know that I've got a string, and I got to replace something in it. So I'm like, what is that replace function again? So, I type that into Google over, and over, and over. Google is my best friend when I'm coding in PHP. Now, once I've been in PHP for a day or two, then I kind of remember the main things. But the documentation that they have online for PHP is outstanding, and very good at getting you what you need. The other thing that's really cool, so if you scroll down on these pages, you'll often find a sample code that the users have provided. And so, the PHP documentation is great. But, you can also download, in case you're on an airplane, or don't have a good network connection, you can download a PHP documentation viewer. And so that's quite useful. So, here's a couple of samples of functions that we're going to use. These are string functions. Strings are not object oriented in PHP. And so, we sort of follow the pattern of passing a parameter in, and getting a result back. And so this is the strrev. And PHP is pretty good at prefixing all these with str as a way to tell you that they are about strings. So this is reversing. Why you need a reversing? I don't really use it. But there you go. It's very exciting because it reverses the thing. Just a repeat. Why we wanna do this? I don't know. But, we're passing in a string and saying repeat it twice. I put a space here so it's hip, hip. Translating something to uppercase, that I do do all the time. Hooray. And strlen which is how long the string is. So that's just an example of some of the string functions that you might use. To define your own function, we use the keyword function, followed by the name of the function, followed by optional parameters, followed by curly brace, and of course there can be further indenting and further curly braces that going on here. And then this is the body the function. And like in all function situations this code is not executing, it's being remembered and effectively expanding the language with a new keyword. So it copies all this into greet somewhere and that's kind of like a variable name as it were. And then you can call it. So this function is defined once and then invoked twice. So it runs it, goes up and runs it, comes back down, runs it again. So it runs twice. Run, run. So there's no output from this because that's the definition of it. You can choose function names. They don't start with a dollar sign. You can start them with the underscore. Again we tend to reserve underscore for sort of library like things. We don't tend to, we tend to avoid underscores in our main code. You can start with a letter underscore, letters and numbers so it's pretty much the same. Avoid built in function names. The case does matter but I think it's a mistake to treat the function called, you know, zap and Zap differently. So don't take advantage of that. That doesn't matter. Why? I don't know. Returned values, much like C-based languages use the return keyword and so it basically says if this function gets called in the evaluation of an expression, so in this case it's concatenating whatever the function greeting returns with the string Glenn and so it comes in, returns it, and so this expression here becomes the residual value that is in that expression. So it just means that once greeting runs, hello is here, and then this concatenation runs and then that goes Hello Glenn. And then it runs again, greeting returns, and it's Hello Sally. So the concatenation thing is Hello Sally. You can also pass parameters in. Parameters have the dollar sign. The function name doesn't have a dollar sign but the parameters do. And like all parameters and functions, the normal thing is that they are, sort of, temporary copy of the incoming and that dollar lang only exists while the function is running. And so in this case we're going to do this concatenation of howdy es to Glenn. So howdy starts running. So es is somewhere out here and lang points to es, then this code starts. And so now that instance, that execution of the function, we're going to see Spanish return Ola, then it's going to be done and then the next time, right, it runs it again, and in this case fr is out here, and lang points to fr, it runs again. That's false but this is true. So it returns Bonjour and so we get the residual value here. It goes back here and we concatenate that. Bonjour Sally. Okay? So this variable is effectively an alias. Another really elegant bit of PHP that I really like is the concept of optional arguments and if you put an assignment statement here in the function definition this becomes the default value. So in this case, if you don't specify it, then it assumes es as the value for lang. If you specify it then it points to fr and runs again. So that's a default. I think it's as beautiful and elegant a syntax for defining the default of an argument as any programming language. This is emphasizing the fact that sort of this variable alias only lives inside the function and it's an alias of the original value. So we have val equals 10, so there's dollar val out here, in the outside world it has a 10 in it. Okay. And then we pass val into the function. So the interesting thing is the way this really sort of works is it kind of makes a copy of the 10 over here, and then it points alias to it, meaning alias is not pointing at that 10. It's pointing at that copy. So now I can actually change it. So I say alias equals alias times two. So that puts a 20 in and that puts the 20 in here. So there's a 20 in this little place, but there's no 20 here. This is separate. So it kind of keeps it separate. It really isolates firewalls, stovepipes, it keeps this function from messing with things outside of itself including its arguments, its parameters. So there's, this is called copy. So the parameters are copied rather than actually referenced. Okay? So in this case if we have val equals 10 we pass it in and we see this dval which is return variable so dval is 20, but the original variable came back out of that, which is unchanged. Call by value. Now. Lots of programming languages that you use are call by value, although some programming languages kind of like, if it's an array it's call by reference, and if it's a scalar, it's call by value. But PHP gives us a call by reference capability. To say that we don't want this parameter to be an alias. We want it to point to the original. And we do that by simply adding a little ampersand. So the function in effect declares its intent to be able to modify that. There's nothing we change in the calling sequence. So what happens here is there's a $val out here. And then we stick a 10 in it, and then we call it triple. And then this says, instead of making a copy, real thing is a second alias to that variable. So when I am going to multiply it by three, ten times three is 30, and I put 30 in the real thing, I'm really changing this variable. This variable is outside the scope of this function. So we sort of have a wall around the function. Oops, but there's a little tiny break in the wall, little tiny doorway in the wall, and we're going out and changing this variable. We can't like sneak into other things but we can touch the actual contents of the value. So that's called call by reference because this actually first to the original variable rather than previous example it's call by value, which means we're getting a copy and we're getting the value in the variable but we're not getting a reference to the variable itself, and so when we come back, we're printing out the 30 because it's been tripled. Call by reference. That's a kind of a more advanced concept. A lot of languages don't even have that. The & came from C that's how C used to do it. The syntax for call by reference is a little bit different because you don't have variables that start with dollar sign. And actually how you use it inside the function, but we're not, this is not a C class, it's a PHP class. Now the reason this is important is as you read documentation you can read through the documentation and you find, if you see this ampersand be like, oh that's interesting. That means, this ampersand means, it's going to change the array. And the interesting thing is if you pass an array in to a function, the default is to call by value. And actually that's different than a lot of languages. Meaning it's a copy. But if you say ampersand, then it's an actually call by reference and you can decide that this function inside this sort function is going to do something to the array. In some languages you actually return the new array. In this, you're going to sort it right there. And so it's just as a reference. And so you've got to be able to read when you see that &s, you're just like beep beep beep beep. They're going to change that. When you pass an array in, it's going to take and change it. So just, you need to know what that ampersand is just so you can read the documentation. So up next we're going to talk about variable scope. We've talked about things that can be inside the function and then outside the function and that's what we're going to talk about next.