Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
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.
A function is code that preforms a specific task, which is then packaged up into a single unit that can then be called upon whenever that task is needed. An example of a function that we've already seen and used would be in array. Any time we want PHP to whether an element is in an array we can call the in array function. We provide the function data to work with in the form of arguments. And the function returns output to us at the end. These are common feature functions, providing them input and getting back output. Up until now, we've been looking at the built-in functions that PHP provides, but we aren't limited to just these functions.
We can also define our own. In this chapter, we'll learn how to craft and use custom functions. And we'll start by learning the syntax that's used to define them. You define a function by first saying, function, and then a space. And then the name that you want to give this function. It's very much like the variable names that we assign. And the rules are very similar. We can have letters, numbers, underscores, and dashes. You can't have any spaces, though. And it must start with either a letter or an underscore. Unlike variable names though, function names are case insensitive. So, with a variable, if we had capital var, and lower case var. Those were two different variable names.
But if we have a capital function and a lowercase function, they're going to be the exact same thing. However, it's really bad programming practice to start switching case around. So, whatever you define them as, that's how you're going to want to always call them as well. Pick one style and stick with it. For me, I like the all lowercase with underscores style. And that's what I'll be using. After we have the name, then we have parentheses with our argument list. These are the arguments that the function is going to accept. In this example, I've just got arg1 and arg2 separated by a comma between them.
Both of these are going to be variable names so they're going to have the dollar sign in front of them. We can have more than two, you just put a comma and have arg3, arg4 and so on. You'll probably want to use a better name for yours that will actually describe what you're expecting to get in. So, if you're looking for an array, the name of it might be array. And then, we've got our curly braces after that and inside that is the meat of the function. Its all of that code that we're wrapping up inside this function. This is the code that we want to reuse and be able to call from a lot of different places. This code tells PHP take this block of code inside the curly braces and keep it around.
File it under the name. And when I call for that name, take the input that I send run the code and give me back the output. Let's try an example. To start with, I will open up basic .html, and I will do Save As and we are going to call this functions_defining.php. And I'll give the title Functions Defining. Alright, so let's write our first functions in our PHP tags. To write our first function, we're first going to use the keyword function.
Followed by the name, and I'm going to call this one say_hello, and then the arguments. Now, there's not going to be any arguments to this one. But I'm still going to put the empty parentheses there so that it's clear that that's where the arguments would go. And then, curly braces. And then, whatever code we want to package up. Whatever we want to happen when we call this function. And I'm just going to have it echo the classic, Hello World, exclamation point. Put a br tag, and a semicolon at the end, okay? So there I have, I've defined my first function.
It is not something that pre-existed. This is something that I'm defining. Let's go to Firefox and take a look. So, I'll open up Firefox. And it's going to be localhost, and then for me, I'm going to use Kevin Scoglund sandbox and then functions_defining.php, okay? I didn't get anything back. Now, I am seeing the right page. And you can tell that because it says up here at the top, Functions Defining, right? So, that is the page that I'm on. And we can actually go to our Web Developer Tools. And we can view the Page Source.
And we can see that we got something. But there's nothing in there. Nothing gets output when you define your function. We have to then call the function. So, what we're saying to PHP is take this code and store it for later, because I may call on it. But we haven't done anything, we haven't called it. It just simply set it aside and assigned it the same way as if we assigned a variable a value. We just assigned a block of code to the say hello function. Now, in order to call it, we need to say hello. Let's try it now. Save that, let's go back over here.
And sure enough, Hello World pops up. Now, the parentheses here are not optional. If we are calling a function, we want to make it clear that it is a function. If we take it away, then PHP sees this as something different. Over here, just reload it so you can see the difference. You can see that we get a notice saying that we are using an undefined constant, say hello. And that's not what we want at all. We were trying to call our function. So, when we call our functions, even if there's no arguments. We're going to use just two parentheses afterwards. So, the correct way to say this is that we are calling the function.
We're calling the say_hello function. Alright, now let's try this with our first argument. Let's come down here and let's define another function, but this one is going to be say_hello_to, underscore to. And then, we'll put an argument in here and that will just be word, alright? So that's our first argument, is word. And then, we have the ability to use that argument inside our function. So for example, I can now have dollar sign word, right? See how that works? So this is now accessible to me inside me.
Just like my loop local variables are accessible inside of loop, well here. The function arguments are accessible inside the function. So let's let's try that out now. We're going to call it with say, hello_to, and we need to provide a word. So let's just start with world, right? Let's go back, save it. Reload the page and there you go. See we get the second time say Hello World. Now, why use arguments? Well, because we have flexibility then. That's the nice thing about arguments is we can reuse that code so we can have it do something slightly different based on the input that we pass in.
So, say hello to everyone. And that returns hello everyone. Now this is a super simple example but regardless of what task we're trying to perform. What we want is to take input and base don that input, get back different output. We don't want the same output every single time. Now, of course, we can't call a function that doesn't exist. Hello loudly. Let's try that. Come back over here, reload it. You'll see that you get fatal call to undefined function on say hello loudly. It's undefined, we didn't define it like we did these other ones. Now, it may surprise you to know, though, that we can actually take this and let's do say Hello Loudly.
And we'll just make this, Hello World, in all caps. Let's go back and try this now. Reload it and it worked but notice I defined it after I called it, right? It doesn't do it sequentially. In php3, it did have to precede calling it, but php4 and 5 actually pre-process the page to find all of the functions. First, now it's still a good programming practice to put them before calling them. Because someone looking at your code, is more likely to be reading from the top down.
And to be surprised that they haven't seen it defined. So, don't get into the habit of defining it later. But it's not going to cause you a problem if you do accidentally put a function after calling it. It's still going to be available to you. Now once we've defined a function, though. We can't redefine it. Just do another say_hello_loudly here. And I'll just change this to we can't redefine a function. Let's just come over her and try that out. And you'll see fatal error, cannot re-declare say_hello_loudly.
Previously declared at this spot. It tells us where it's located. So, we can't do that. So, I'm just going to comment that out so that won't cause our page to break anymore. Now, you can define functions inside other functions inside if statements, inside loops. But this is really advanced usage and can lead to many problems and unexpected results for beginners. So for now, be sure that you're always defining your functions at the quote root of your PHP page, not inside any other PHP structure. Now that we understand the fundamentals of defining functions, let's explore how to use them a little further in the next movie.
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.