What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Next video in… 10

Loops

Functions

PHP Fundamentals

Functions
Master function creation and usage in PHP, including modern features like type declarations and anonymous functions.


Functions are essential building blocks in PHP. They help us organize code, make it reusable, and prevent repetition. Let's explore how they work and what makes them special in PHP.

Basic Function Syntax

Creating a function in PHP starts with the ⁠function keyword, followed by the name and parentheses. The name should describe what the function does:

1function sayHello() {
2 echo "Hello!";
3}
4 
5// Call the function
6sayHello();

Parameters and Arguments

Functions become more useful when they can work with different values. We can pass data to functions through parameters:

1function greet($name) {
2 echo "Hello, $name!";
3}
4 
5greet("John"); // outputs: Hello, John!
6 
7// Multiple parameters
8function add($a, $b) {
9 return $a + $b;
10}
11 
12$result = add(5, 3); // returns: 8

Return Values

Functions can send back results using the ⁠return statement. A function can return any type of data - numbers, strings, arrays, or even other functions:

1function getStatus($score) {
2 if ($score >= 75) {
3 return "Passed";
4 }
5 
6 return "Failed";
7}
8 
9$status = getStatus(80); // returns: "Passed"

Default Parameter Values

PHP allows you to set default values for parameters. If an argument isn't provided, the default value is used:

1function greet($name = "Guest") {
2 echo "Hello, $name!";
3}
4 
5greet(); // outputs: Hello, Guest!
6greet("John"); // outputs: Hello, John!

Type Declarations

Since PHP 7, you can specify types for parameters and return values. This helps catch errors early and makes code more reliable:

1function add(int $a, int $b): int {
2 return $a + $b;
3}
4 
5function getUserName(array $user): string {
6 return $user['name'];
7}

Anonymous Functions (Closures)

PHP also supports anonymous functions, often used for callbacks or when a function is used only once:

1$greet = function($name) {
2echo "Hello, $name!";
3};
4 
5$greet("John"); // outputs: Hello, John!
00:00
Functions are essential building blocks in PHP to help us organize our code, make
00:05
it reusable and prevent repetition. So let's explore how they work and what
00:09
makes them special in PHP. We're going to create a new function with the function
00:13
keyword. Then we have the name. Let's just go for create here. Then we have the
00:18
parameter list but let's keep this empty for now. And then we have curly braces,
00:22
opening and closing and inside here we have now our function body. And inside
00:29
here we define what should happen here. So let's start very simple with an echo
00:32
hello. Here we go. And then in order to call this here we can just go with
00:38
create empty parameter list and semicolon. And this should do the trick.
00:42
Yes we can see hello. So you just wrote your first function in PHP. But of course
00:48
this would be maybe a little bit too simple. So what do we want to do? Let's
00:51
add a parameter. So let's go backwards. So how do we want to make this work in the
00:57
end for the user? So you want to create a specific person. So maybe it's just me.
01:02
I'm going to provide here my name. And then inside the function declaration
01:06
here I'm going to tell here we have a parameter which is let's call this name.
01:11
And then we're going to use this here. And we already learned that we can use
01:15
double quotes here instead of the single quote in order to make this variable
01:19
work inside the string. And if we run this now we should see hello Christoph.
01:24
And yes we do. Okay we're starting again by creating a new function. We're going
01:28
to call this add. And now we have two parameters. First we have our first
01:32
number. And then we have our second number. And then we are creating our
01:37
function body here. And here we're going to return something. So we're not going
01:41
to echo something out. We're just returning something. And we're going to
01:45
return number one plus number two. All right. Let's call this now. We're
01:53
providing now two numbers. Maybe one and five. And let's see what we get back.
01:58
Nothing because we also need to echo the result out. Or we can store it in the
02:02
variable. But in our case we're just echoing out. And yeah you can see this is
02:06
also working. And you can also see it doesn't matter if we're going to return
02:10
a string. Or in this case it will be an integer. We can return just anything we
02:14
like. An array or whatever. Or we don't have to return anything at all. This
02:19
would also work. Okay back to our creating example here. We had Christoph here.
02:24
In order to create myself. And it's still working. Yes it does. But what we can also do
02:29
is we can provide default values. So let's imagine I'm not providing here any
02:34
argument. You will see that this is now failing. And it's failing because there
02:39
are too few arguments passed to the create function. We can fix this by just
02:43
providing an argument and parameter here. But what we can also do is we can
02:47
provide a default mount by giving this inside this parameter list here a value.
02:52
And let's just say by default this should be just you. And you can see this
02:56
is back working saying just hello you. Since PHP 7 also the type system in PHP
03:03
has become way better. Now we can type a lot of things here in PHP. So for
03:08
example for our add method here we could say that the first argument and the
03:13
second argument should be an integer. We're just providing here the type in
03:17
front of the parameter. And we can also see that the return type should be an
03:21
integer as well by using this syntax here right after our parameter list. And
03:27
let's sort this out. It should still run. Let's provide two values here. We get
03:31
nothing back because we need to echo it out again. And it's still six. But of
03:35
course it would fail now if we would try to return a string which is not a number.
03:41
You can see this is now failing because here we've made sure that we want to
03:46
return an integer. And the same would happen or something similar if we would
03:49
provide to add something here to this method which is not an integer like we
03:54
have defined it here. And yeah this helps a lot being very strict about your types
03:58
in PHP. You can use them if they want to. You don't have to. But yeah I myself I'm
04:03
a big fan and also in Labo we use them a lot. All right one more thing before I
04:09
finish this video. So in the case of our greeting example we could also create now
04:14
a greetings variable. And let's bring in our function from before again. So we
04:19
want to store a function inside this variable. And we can do this with
04:22
anonymous one. So this means we don't need a name here. We're just providing
04:26
the function itself. And then we need to end the statement. If we just run this
04:31
code you will see nothing is happening which is a good sign. But if we now run
04:36
this function like we would do before but now with the variable. And we don't
04:41
provide any arguments here. You can see this is still working and it will also
04:46
work. Now trying to greet Taylor. Hello Taylor. Yeah so this is also working. At
04:52
the first glance this might look a little bit strange and you're not sure
04:55
how you would use this. But I tell you this is something very powerful and we
05:00
will use it a lot especially with Labo together. Because this makes writing
05:05
PHP and then Labo a lot of fun. And yeah that's it for now about functions. We
05:11
covered the basics here but of course there's much more for you to learn about
05:15
them. And we're going to talk about them a bit more in detail when we talk about
05:19
classes. Because this is mostly where you're going to use them.