Check out a free preview of the full PHP Basics course
The "Functions" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano introduces PHP functions. The function keyword is used in the declaration. Arguments are prefixed with a dollar sign like with variables and can have a type specified. This lesson also demonstrates the new named argument feature of PHP 8.
Transcript from the "Functions" Lesson
[00:00:00]
>> Maximiliano Firtman: We have one more thing to cover before getting into the web. Let's talk about functions. That is also something that we use a lot when we are working with with code. So I'm going to create a new file. Let's call that functions.php. I'm going to play with functions a little bit and then we will get into a web server.
[00:00:20]
Remember, we always need the php tag. Functions are pretty simple, but I just wanna mention a couple of things that you may not expect. So how do you create a function? Actually, we are using kind of JavaScript syntax. So we use the function keyword and the name of the function, like printHello and no dollar sign.
[00:00:42]
Okay, dollar sign is only for variables. So a function printHello and it can, for example, do an echo, the Hello World. So pretty simple, straightforward. How to call the function as you are expecting? You just call the function as will most of the languages. Okay, so nothing really important there.
[00:01:04]
So of course, you can receive arguments. So for example, I can make or calculate tax and we are going to receive a price and here comes the important part, this is a variable. So this is PHP, it's a variable, should be dollar price, and we receive, for example, the tax and then we can return, as with JavaScript and other languages.
[00:01:31]
So I can return, for example, the price multiply by using the standard arithmetic operators as you're expecting like that, okay? So straightforward. So to call calculateTax, you just pass the price. And for example, the tax, 10%, okay, nothing important. But let's start with something that we can add.
[00:02:01]
And some of you that were actually complaining, I actually, I'm hearing your thoughts. And some of you are complaining about the lack of type declarations, for example, mostly if you're coming from TypeScript, Java, C sharp, Kotlin and so on. And well, on function arguments, we can specify data types, not on normal variables, but I can reinforce that I want, for example, numbers, not strings here.
[00:02:34]
So I can say that I want an integer. Maybe integer is not the right one for now, but I will change it. But I can say this, and we use for that some kind of a C syntax, okay? So the data type goes first, space, and the name of the argument.
[00:02:51]
What are the possible types that we can use? Well, I mean, it's not a big list. When you are using classes that we will talk about OOP later, there are more, but the possible types are bool, not boolean, bool, lowercase, int, float for floating numbers. I think for our case, we should be using float, string, array, object, and callable.
[00:03:20]
There are a couple more that are specific for some weird situations, but callable is actually something that you can call that's actually a function. So you can receive functions as an argument, like a callback, object, it has to do with OOP, we'll talk about that later, and the rest are pretty straightforward.
[00:03:38]
So in this case, I can say that I want to receive a float. What happens if you don't receive a float? Well, it will generate an error, okay? And we'll talk about the type of errors by the end, but there are different levels of errors in PHP and in this case, it might break your app, okay?
[00:03:58]
So we are actually forcing the color of this function to use that particular type. Okay, any questions on that?
>> Maximiliano Firtman: So we do have types on some special places such as function arguments. Okay, so what else do we have on functions that might not be so straightforward? Of course we can, you saw that we call the function as we are used to, but instead of using that syntax, we can change the syntax, and we can use name arguments, this is new in PHP.
[00:04:38]
So instead of using positional arguments, that is sending values by position, I can use name arguments. I'm not sure if you have heard about name arguments on other platforms, like you have that on Kotlin, on Swift, kind of. It's not exactly that, but you can kind of do something like that.
[00:05:03]
But what's the idea? Well, when you are calling the function, you can say, well, you know what, I can call the function passing the price, oops, inside parentheses, the price is gonna be on the price, okay? So, 3000, and then the tax is gonna be under tax, okay, make sense?
[00:05:28]
And also you can change the order. When you use name arguments, then you don't need to use the same order, okay? Does it make sense? So that's kind of the idea. So you can say the tax first, and then the other one goes after, like that. And when we are using name arguments, we don't use the dollar sign.
[00:05:54]
Okay, so we don't use the dollar sign. And we can change the order.
>> Maximiliano Firtman: Something that we have also is, we have default values. So I can, for example, receive something as default, so by default it's 5%. So then now I don't need to pass the tax on both situations, okay, cuz we do have a default value.
[00:06:17]
And you can also match and mix here. So for example, you can have a name, that the name is a string, the name of the, tax name, let's say tax name. It's common that something, you didn't ask but what's the guideline, naming guideline for variables? And actually in php, you have all of them applied at the same time.
[00:06:42]
Should we use camel case for variables? Should we use this one, snake case? It's called a snake because it looked like a snake. Someone thought that this is a snake or looks like a snake, okay? Kebab, Kebab, well actually, we cannot use kebab case, kebab case when you use dash.
[00:07:02]
That's the CSS syntax because we cannot use dash as an identifier, but we can use title case. Actually, it's up to you. And within PHP, within the library, there is a mix. All of them apply at the same time. So actually, it's up to you. Well, let's say we have a default value, the thing is that I can pass, for example, this is our positional base argument, because it's by position, not by name, so I didn't add price.
[00:07:37]
I didn't add the prefix, okay? So if I go in order, I can start with positional base arguments and then jump into name arguments. So I can just change the tax name here. It's on our state tax forever. And you can see I'm kind of skipping the tax, the one in the middle.
[00:08:01]
>> Maximiliano Firtman: It's possible. The only restriction to mix positional arguments and name arguments is that you should start with position as always. If you start using or after you start using a name argument, everything should be named.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops