From the course: Learning PHP

What are functions? - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

What are functions?

- [Instructor] Functions are reusable snippets of code that can be called multiple times. You can imagine functions as variables for blocks of code. And the reason that we would use functions is when we want to reuse a snippet of code more than once, instead of rewriting those multiple lines of code every time, and then having to change them if we ever want to update those lines of code, we can put them in an easy to use function. Functions can do one of two things. They can return a variable or a value that can be used as or assigned to a variable, or they can print something out. Here's the function syntax for printing something out. We have the keyword function and then the name of our function, the name of the function should be formatted similarly to the name of our variables, where spaces are replaced by underscores. But notice there's no dollar sign in front of the function name. Instead, the function name has an opening and closing parentheses after it. Then we have our curly braces and in between the curly braces, we have the code that we want to execute as the function. In this case, our function is printing out Hello World, and you could see it in use below. So we have our paragraph tag and then our opening PHP tags. We have the function name, Hello World with the parentheses and a semi-colon, our closing PHP tag, and then our closing paragraph tag. This will print out a paragraph with the text Hello World. A function can also return a value. So again, we have our keyword function, the name of the function, Hello World, with the parentheses, the curly braces. And the only difference is that instead of the word echo, we have the keyword return, which basically says treat whatever you are sending back from this function as a variable. So in this case, we are using the word echo. Then we have our paragraph tag in quotes, the concatenation operator, our function call, Hello World, a concatenation operator, and then our closing paragraph tag with the semi-colon. So in this case, the function name, Hello World is being treated as a variable with the value Hello World. If a function were to return a value, it could be treated as its own variable or assigned to a separate one. So let's take a look at a simple example where a function returns a Boolean. We have a function here called is bigger and inside it returns the Boolean operator 10 is greater than or equal to five, which will be true. Then below that we assign our variable bigger to whatever is returned from is bigger. So we're saying bigger gets is bigger, the function. And then we can use that variable and an if statement, but we can also just call the function as the condition is the if statement and skip the variable assignment altogether. So we have, if is bigger than echo, one thing, otherwise echo another thing. So here the function is being treated as a variable. Finally, you might be wondering why we have parentheses for the function name. Well, we can pass variables or arguments to functions. So our is bigger function is pretty useless if it's only evaluating 10 is greater than or equal to five, we might want something more dynamic than that. So you could see the syntax here, function is bigger, and then inside those parentheses, we have two arguments, A and B. A and B will take on the value of whatever we pass to the function. So in our assignment, we have bigger gets is bigger and then 10 and five. So in this instance, 10 will be assigned to A and five will be assigned to B. And functions can do more than just return Boolean values. Anything that can be stored in a variable can be returned from a function. The last thing it's worth noting here is that functions need to be defined before they are called, just like variables. So if we were to swap this syntax and have bigger gets is bigger before the function declaration, PHP would throw an error because it doesn't yet know what is bigger is now. This is how we rate our own functions, but PHP has a bunch of built in functions as well. You've seen a few of those already, like size of and strlen. These will help you accomplish tasks very efficiently, like formatting the date with the date, grabbing the size of an array and more. Let's look at PHPs' built in functions now.

Contents