Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
All of our functions, so far, have been using echo to send output to the user's screen. But more often than not, we don't want that behavior. Usually, we'd like to get a result back from the function, and then decide what to do with that result ourselves. We might end up deciding to echo it to the screen, but we might instead take that value and continue to use it to do more processing. If it outputs from inside the function, then we lose that flexibility. It's better to get back a value. And for that, we need to understand, how to return values from a function. Let's open up basic.html, and we're going to do Save As on this.
We're going to call this one functions_returnvalues.php. (SOUND) Functions: Return Values. So let's just write a simple function here. I've got an add function. It takes two arguments value 1 and value 2. It adds them together and assigns them to sum. So let's just load this up in Firefox to begin with. Let's bring this up. And instead of functions arguments, instead now we're going to be loading up return values with no space in between.
Alright, I get nothing back. You can see it did load the page, but all I did was define the function. I haven't actually called it yet, so let's try calling it now. Add 3,4. All right, let's save that. Let's go back and reload the page. Still didn't get anything back. Why not? Well, it's because we're not echoing anything from inside the function like we did before. We could put an echo here, echo $sum and that would now output a value, but that's not as flexible. That doesn't return the addition to us so work with it.
The only option it gives us, the only way to get a value out is to print it to the browser. Now we could try printing this value from outside the function. Let's try that. Lets do after we add them together, let's echo that value. And it comes up and says nope undefined variable, because sum is a variable that's only inside the function. It doesn't exist outside the function. So we can't use this as a way to get values out of there. Instead, what we want to do is we want to return a value from the function. So we would have return and then what we want it to return, sum. So now it'll return sum to us.
And then, we need to catch that result here by saying the result is equal to Add 3 and 4. Now, we get back the result and we have the ability to echo that result to the screen. Let's take a look at that. And now we get the value of 7. Now, we don't have to echo this to the screen. We could, instead, do more operations with it, right? We could have, now the result is to take 5 and add that to result, what result was previously.
I can actually name these results 1 and 2 if that makes it a little clearer instead of overriding the other one. So result 2 know is equal to result 1 plus 5, come back here, and we get 12. Right. Much more flexible. We have the ability to output it or the ability to keep working with it... The best practice when you have functions is to always have a return value. Explicitly, say, what does this function return? Maybe what it returns is null. Maybe what it returns is the original input again. Maybe your, your input was an array, and so the output is to output that same array again.
But you always want to return something from your functions. The PHP functions have return values. All of those are listed on the PHP.net website. There's usually a section that say's the return values. It tells you what the inputs are and it tells you what the outputs are, so you can look those up. Now the return also exits the function immediately. It's similar to how a break works. Where break just got us out of a loop immediately, or out of a switch statement immediately. Well, return works the same way for functions. It says, alright, just stop whatever you're doing.
Let's exit the function and return this value. This is the thing that needs to be passed out of the function. No more processing required. And even if we put it inside loops or switch statements that are in the function, it will immediately exits from those as well. So, let's try an example here. Let's go back. We did the Chinese Zodiac before. Let's paste an example using that again. Should put a br tag here. I've defined the Chinese Zodiac as a function. So, here it is, Chinese Zodiac, we pass in a year. And then it takes that year and does the same processing we did before.
But now, instead of having assignment to a variable followed by break. Instead now, we're just going to return the value, that's it. So, case 0, return Rat. Done. No break required. And then, let's jump down here, and let's actually call our function. So let's say, $zodiac is going to be equal to chinese_zodiac. And let's put in 2013 as the year. And then after we've done that, we'll echo back a string, 2013 is the year of the, and put the zodiac here. I'll end it with a period.
And it'll be our tag. And a semicolon at the end. Alright, so let's bring that up and try it. There it is. See how that works? Now we have a function that (INAUDIBLE) wraps all of this logic for us. So now, whenever we want to call this, we can just simply call it with the new number. Let's say that we're looking for 2027, 2027 is the year of the, what? Goat. Now we don't have to assign this to a variable here. Sometimes it's cleaner to do it that way but I can do this in-line as well. We can't do it with the curly quotes but we can do it if we break those and use append and then we can just put it right there.
See how I've done that? Let me just widen my window a little bit. So I'm concatenating there. 2027 is the year of the, then call the function and then the br tags. Let's save it and go back and check that out. And there it is. It looks exactly the same. Now, by the way, I think it's a good practice to rarely have functions which output directly from inside the function. Now that's what we've been doing before now as examples because we didn't know how to do return values yet. But most times I think it's better to build up text inside the function and assign that to a variable. And then, return the value of that variable at the end. Then we can echo that returned value if we want, or we can do something else with it.
But it's better, I think, to leave echoes out of your function. So, for example, we could redo our hello function that we had before. (SOUND). Right? And we had better_hello. Instead of echoing from inside that function, just change it to a return. Return that result, and now, we can just as easily call echo better_hello, with Hello, and then, John Doe, followed by an exclamation point. Right? We can still call it that way but see how much more flexible this is.
We could take this and we could call up case on it or title case or something else before we output it. We have more flexibility. So I think it's better to leave echo statements out of your functions and instead use return values.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.