Hey guys, in this video we will be looking at functions in PHP. Now following our previous video with string and date time manipulations, we would have seen that quite a few functions are actually available to us in PHP. But then there are times that we want to actually create our own functions because well, we have to repeat a task. And we don’t want to type that code every single time. So a function is a perfect way for us, the developers to actually put some bit of code that we may want to do repeatedly inside of some constructs that makes it easier to just make reference to. So as usual, we start off by creating our link to our new page and also our page. Having done that, we navigate to our newly created page, and then we go ahead and include our html5 boilerplate. Next week, fill out the title and set our h1 tag. So I’m doing gone ahead and done all of that and opened up our PHP tags. Now, the first thing that we want to look at is defining a function. So I’m just going to make a comment here. And so we know exactly what we’re doing defining a function. Alright. And then a function, as I said, is a way to actually have your code one time in some construct that allows you to make reference to it on demand. Now, to get started with defining a function, what we have to do is use the keyword function. So we were at a few NC t IO n, and then we give this function some name. So I always recommend that you name your variables, well, you know, usefully according to what you’re storing in them. So in the same way, keep your function name kind of useful to what it will be doing. So if I’m going to write a message, I’m just going to see writes message and then I need an open and close brace. All right, and then I need to open close curly braces. All right, so this is a typical form of any function, you have the keyword function, you have the name of said function, and then you have open and close parentheses. So at the basic level, that’s what you need. And then whatever it is, this function we’ll be doing will be inside of these curly braces. So let’s say this function is supposed to print a phrase and say you are a really nice person have a nice time. So inside of these curly braces, I’m just going to write my echo statement, and then I will put in my expression. Alright, so if we save this, and then we load our page and take a look, we shouldn’t see anything except our h1 tag. And that’s because even though we declared sorry, even though we declared the function, and we said it should echo the function itself is not being called it’s just defined. So you can actually define this for Shaun with whatever action it should carry out, but this will never get executed unless you explicitly call it. Now this brings us to our next task, which is actually calling this function. And calling a function is probably the simplest parts of this exercise where literally all you do is just reference the name that was given to the function. So we saw where we wanted to turn a string into all caps, and there was this function s to str to upper, we can always go back and look at it, right. So all of these are examples of functions. We didn’t define them though, but we’re just calling them so we’re passing in some value, and calling the function and then it is doing something in the background. So that’s what I mean by a construct that will do something without you needing to repeat your code, except these were pre existing and now we’re looking at making our own or having user defined forms. So to call a function, we follow the same principle where we just reference a function by name, which is right message. And then we call it if it has parameters. In this case, there are no parameters. So we just open and close empty brace, and then a semicolon. And then what this will do is actually call the function and run that echo statements. All right, now I’m going to just modify this echo statement and put in a br tag here. Alright, and then I’m just going to show you that anytime you need to actually print this or do that you can just make a reference to this function whenever you want it. So if I wanted to print this five times, I can just call the function five times instead of echoing this statement five times and when I refresh, and that’s what I put in the br tag so that it would all come in different lines each time. But each one of these is just a function call and I can do anything I want to do in between the If I wanted to have a line in between, so maybe I encode an HR tag, right, and then after that HR tag, I wanted to write message again. And I’m just going to copy and paste the function call. Alright. And if I go back, then you will see that I have one function call something else, and then another function call. So there is no limit to the number of times you can call this function. Now one thing to note, it is always a good idea to define your function before you make reference to it. If you attempt to do it in the other way in the order, then you might run into problems. So it’s always good to have your function definitions at the top of your page, and then the rest of the code down below, because then when the page renders and it hits this line where it’s referencing it, it knows that it was already declared up top. Now let’s look at defining another function and this one Has parameters. So I’m just going to create a new search on here. And what I’m going to do is define a function and we’ll call it add numbers, right. So I write my keyword function, and then I’m calling it add, let’s say add function. So this function is going to add two numbers. Now the two numbers that I want to add, I want to be able to get that from my program, I don’t want to pre determine those. So I’m just going to put in my parentheses in the parameter listing a space for number one and number two. So I did mention that you have parameters. So if we look back here, whenever we passed a value into the function call, that was us passing in a parameter value, okay. So in the function definition for you see, first word, you see it has function. You see for And then it has some variable called str. So that is the parameter that it was defined to look for. However, it is just saying that you need to pass in something here that is off some form of string value. All right. So in this case, I’m just hinting at my users, whenever you’re going to use this function called add function, I need a number one and a number two, it has to be both I have to get two numbers in order to carry out the action. So then I want to go on and I’m going to see some, and we’re going to do a bit of math here is equal to num one plus num, two. And then at the end of that, I’m going to echo and I’m going to get a bit creative with my equal statements. And I’m going to see the sum of normal ones one and number two, He’s cool on. And then I’m going to print the variable sum. So I’m using my interpolation here. And I’m getting some number here some number, they’re doing some math where I’m adding them and storing them in another variable called sum. And then I’m going to say echo, the sum of that first value, and that value is and then have that final value. And then I’m just going to print a br tag after that. So in case I call the function multiple times with multiple values, it will all print in different lines. Alright, so now now that we’ve defined our function, and it’s called add function, I’m actually going to go down and I’m going to call add function. So what if I wanted to add two numbers? Maybe 10, and 20. And then let’s see what happens. So I go back to my page, refresh. And there we go. sentences printing printed seeing the sum of 10 and 20 is 30. So the interpolation is working, the sum of whatever value was passed in as the first variable, or the first parameter, which is 10. That’s what gets stored in this variable called num one. And then that passed in 20. And that’s what gets stored in num two. And just the same way, if I had a variable, and I call it just num, and I assign 500 to it, then I can call add function again. And then I can see I want to add 10 to whatever value is inside the variable num. So it doesn’t have to be a static value. This is what we call a static value. It can be a variable, alright, as long as it is a number, because what I’m doing has to do with numbers. So if I pass in a string, then it is probable that it would break the function. So let’s experiment with that. What if I pass In a string, instead of number 10, let’s see how that would work votes. Let’s pass in two strings. So I’m going to pass in 50, and 10. And I’m using the two different quotation marks. So let’s see how flexible PHP is or is not. And if we look at what’s happening, it’s printing out the sum of 10 and 500 is 510. And even if we pass in a string, it’s still kind of doing the math part. As we discussed earlier, PHP is not very strongly typed, so it’s not going to hold out ransom for if you use a quotation mark. Sometimes with a number, it’s still going to kind of process it and contextually say, Oh, well, even though you pass in a string, the string is a number and you are adding so I can still do this alright. So that is how a user defined function with parameters is defined. and by extension, this is how it is called. Now on the flip side, you can actually pass a parameter into the function? Well, we just did that. So this is what you call a passing by value. All right? So we have some value here, and even if it’s another variable, but we are passing it in by value, and there is nothing I can do in the function to actually change the original value of num. So even if I said here, that num, is now equal to whatever value num is plus one, which means if Nova is 500, then at the end of this statement, num should be 501. All right. And so even if I’m passing it in by value, and I’m trying to do that, well, that that really wouldn’t work. How sorry, say not to, right, sorry about that. So because I’m passing the value of num into the num two parameter, then I would have to try and manipulate num To hear. So if it was a case where I wanted to actually change the value of num using the function, well, what we’re doing as passed by value is not going to do that, because the value of num will not change. So to prove that if I go ahead and echo num underneath this, then I’m expecting to see 500 get printed again, because the value of the num would not have changed. So if I refresh, that, I’m still seeing 500. And it’s not it’s not space, because I didn’t put in the br tag. Now if I wanted to be able to modify the value of a function, or sorry, have a variable that is not defined inside of a function, then I have to do what you call pass by reference. So I’m just going to define another function. And once again, there is no limit to the number of functions that you can have. And let me just say this one is going to be called change. No. All right. And it’s going to take a parameter called num, right? Because what I intend to do is pass the pass this value in to this function. Now the tool can have the same name, but as you see up here, you can have different names because this is number one and number two. So once the valley hits one of these, it is no identified by the name number two, but if you use the same name, it’s of no real consequence. So this is actually going to just say echo. But it’s really just going to see num is equal to num plus 10. All right, so my objective is that after I call this function, and I’m going to pass this variable into that function, this variables value should actually change not change here, but then not change at all. See, so initially, it changed inside of the function, but the original value remained. What I want to do is allow the function to actually change the value of it from outside of the function. All right? So the objective is that, Okay, I’m going to remove this. And then I’m going to call this function. So I’m just going to say change num. And I’m passing in my variable num. All right. Sorry. And then underneath that, I’m going to echo No, I know just concatenate this with a br tag. So I hope you see that there are many ways to do this BR tag and the string and the printing and so on, because you could do it all in one line with interpolation, you can concatenate it. And this is really a number being concatenated onto a string. So there are quite a few options available to you. Alright, so back or objective I am trying to make change num, change the value from being 500. All right. Now what I need is what let’s see what this does first. So what’s going to happen is that change them is going to get called. And then it’s going to pass in the value that is stored in gnome, which is 500. And then change them is just going to say 500. Well, this variable is no equal to whatever its family was plus 10. And a short to iterate this, I’ll just write it underneath is actually num plus equals, and then the value. So that’s a more succinct way to write this tire. So I’m seeing 500 plus 10. And whatever value this comes up to, no, you store that in that variable. But what we’re going to see is that even though we’re doing this modification, by the time it hits outside of the function and echoes the value of num, it’s still going to be 510. see exactly what I mean. So you see, it’s still printing only 500. Know what our objective is, is to make it print the end result of what is in the function. And once again, the only way to get it to modify that is to pass it by reference. So what we need to do is add what we call an ampersand, which is the unsigned, which I believe if you’re using a Western keyword, at least it’s shift, and seven, that’s an ampersand you pass that in before the variable. And what that does is passed the reference storage area of the variable, so then it gives the function the ability to modify the original value of that variable. So passing by value is just like copying and pasting. Okay, think of it like a bank account. So when you pass by value, that’s like me telling you that I have $10,000 in my bank account, that doesn’t mean anything to you. Because you can’t touch my bank records. You can’t change the value of my bank. iPhones just by me telling you it’s $10,000. So that’s what we call a pass by reference. However, when you pass by, sorry, that’s what you call pass by value. However, when we pass by reference, that’s like me giving you my debit card, and the pin and my bank account number and my login for my bank account online. And then you can go and you can withdraw money and actually effect a change on my bank account. So that is what we call pass by reference, where we actually include this ampersand, so it says give you give the function all the details of the variable, and pretty much that variable can be modified inside of this function. So even when the function execution is done, whatever modifications occurred still obtain outside of this function. Now this pass by reference can be done to as many variables as you need. So even here, we could have just included the ampersand and if We wanted to change the values of number one and number two, we could have done that there. Alright. The final thing that we’re going to look at the with functions would be how to set up a value returning function. And an example of a value returning function would be one like, well, any of these string manipulation functions. So what is actually happening, let’s take repeat string, for example, is that str repeat is getting parameter number one, a parameter number two, and if we hover over it, you’ll see that it is input and multiplier, alright, and it is getting those two and then it is actually returning the end result, which is why we could just concatenate it onto the string. So what I’m going to do is create a function and I’m going to call it return, let’s say product. Alright, so we’re going to do some math again. So we’re going to take variables number one, and number two, and then you would notice I’m still using no one on them too. But we already have them here. And I think we would have established that you can’t declare two variables with the same name. So what happens is that a variable is only alive inside of the curly braces of a function, and we call that a function scope. So inside of the curly braces, then this variable exists. Once that function call is done, and the in the program jumps out of those curly braces, that function no longer exists. So that’s why I get I’m able to use the same names multiple places inside of my function parameters. Alright, so I’m going to open my curly braces or start defining the scope and scope of actions for this function. And I’m going to say prod is equal to num one, multiplied by num, two. All right, so you don’t use the X in PHP for math to use your asterisk. And then I’m going to have another keyword that says return and then returning the product, I’m returning whatever value comes into this and returning it. So when I call this function, I have a few options available to me, I can echo and echo whatever values returned here, I can store it in a variable. So let’s try that variable option. So I’m going to say return value is equal to, and then I’m going to do my function call to return product. And I’m going to spring in let’s say 10 and 200. So once again, the expectation is that I have a variable called return value, and its value is going to be set to whatever the value is that is being returned by this function call and this function call getting the parameters 10. 200 scotoma seven colon is going to go ahead and get the product of 10 and 200 or 10 times 200. And then it’s going to return that. And then whatever returns, it’s stored inside of this variable, which means if I wanted to echo the value of this variable, I can just borrow this line of code. And then I’m going to echo return value here underneath the function call. So once again, I have a variable, and it’s going to set its value to whatever is returned from this function call. The function call is returned product is getting two parameters, two values as parameters. And it is defined by us the user to accept these parameters, get the product and then return that value. And then I can echo that value. So if I refresh my page, then I will see I’m getting back that tool fells and end results. Now there’s a lot more to come with looking at functions in PHP. This is the tip of the iceberg what it gets far more fun than this. I hope you stick around and if you have any questions, feel free to reach out.