PHP Functions

Summary: in this tutorial, you will learn about PHP functions and how to define user-defined functions.

What is a function #

A function is a named block of code that performs a specific task.

So far, you have learned how to use built-in functions in PHP, such as var_dump() that dumps information about a variable.

In this tutorial, you’ll learn how to define your functions. These functions are called user-defined functions.

Why do you need functions in the first place? #

Sometimes, you need to perform the same task multiple times in a script. For example, suppose that you want to show a message that welcomes a user like this:

<?php

echo 'Welcome!';Code language: PHP (php)

Try it

If you want to show the same message in other places, copy and paste the above statement in many places.

But when you want to change the message from Welcome! to Welcome Back! you need to change it in various places. It’ll make the code very difficult to maintain.

This is where functions come into play. A function allows you to assign a name to a code block and reuse that code block in multiple places without duplicating the code.

To define and call a user-defined function, you use the following steps:

First, define a function called welcome() like this:

<?php


function welcome()
{
	echo 'Welcome!';
}Code language: PHP (php)

Then, call the welcome() function in any place where you want to show the welcome message:

<?php

function welcome()
{
	echo 'Welcome!';
}

welcome();Code language: PHP (php)

Try it

Later, if you want a different message, you can change it centrally in the welcome() function instead of in multiple places.

Using a function, you can reuse a code block and make your script easier to maintain.

Define a function #

To define a function, you use the following syntax:

<?php

function function_name() {
    statement;
}Code language: PHP (php)

In this syntax:

  • First, specify the function name followed by the function keyword. The function’s name must start with a letter or underscore followed by zero or more letters, underscores, and digits.
  • Second, define one or more statements inside the function body. The function body starts with the { and ends with }.

Like the above example, you can define a function called welcome() as follows:

<?php

function welcome() 
{  
    echo 'Welcome';
}Code language: PHP (php)

In this example, the function name is welcome. The welcome() function displays the welcome message.

The welcome() function doesn’t have input. It shows the welcome message.

In practice, functions often accept inputs, which make them reusable and more useful. These inputs are called parameters.

A function may have zero or more parameters. To add one or more parameters to a function, you can use the following syntax:

<?php

function function_name(parameter1, parameter2, ...) {
}Code language: PHP (php)

Inside the function body, you can use the parameters like variables. Parameters are the local variables.

For example, if you want to welcome users by their usernames, you can add a $username parameter to the welcome function as follows:

<?php

function welcome_user($username)
{
	echo 'Welcome ' . $username;
}Code language: PHP (php)

The welcome_user() function has a parameter $username. It displays a welcome message to the user by concatenating the Welcome message with $username.

Call a function #

When a function doesn’t have any parameter, you can call the function by using its name followed by parentheses like this:

<?php

function_name();Code language: PHP (php)

For example:

<?php

function welcome()
{
	echo 'Welcome!';
}

welcome();Code language: PHP (php)

Try it

The welcome() function shows the following message:

Welcome!Code language: PHP (php)

And when you call the function with parameters, you need to pass arguments into it:

The following example calls the welcome_user() function:

<?php

function welcome_user($username)
{
	echo 'Welcome ' . $username;
}
welcome_user('Admin');Code language: PHP (php)

Try it

In this example, we passed the 'Admin' argument to the welcome_user() function. The function displays the following message:

Welcome Admin!Code language: PHP (php)

Inside the welcome_user() function, the value of the $username is 'Admin‘.

If you pass another argument into the function, the message will change. For example:

<?php

function welcome_user($username)
{
	echo 'Welcome ' . $username;
}

welcome_user('Guest');Code language: PHP (php)

Try it

Output:

Welcome Guest!Code language: PHP (php)

Parameters vs. arguments #

The terms parameters and arguments are often used interchangeably. However, they’re slightly different.

When you define a function that accepts inputs, you specify the parameters. In this example, $username is a function parameter:

<?php

function welcome_user($username)
{
	echo 'Welcome ' . $username . '!';
}Code language: PHP (php)

An argument is a piece of data that you pass into the function when you call it. In the following function call, the literal string 'Admin' is an argument:

<?php

function welcome_user($username)
{
	echo 'Welcome ' . $username . '!';
}

welcome_user('Admin');Code language: PHP (php)

Try it

Return a value #

A function can return a value. To return a value from a function, you use the return statement:

return value;Code language: PHP (php)

The return statement immediately ends the execution of the current function and returns the value.

The value can be a literal value like a number and a string. Also, it can be a variable or an expression.

The following function returns a welcome message instead of displaying it:

<?php

function welcome_user($username) 
{ 
    return 'Welcome '. $username . '!';
}Code language: PHP (php)

Since the welcome_user() function returns a string, you can assign its return value to a variable like this:

$welcome_message = welcome_user('Admin');Code language: PHP (php)

Or you can display it:

<?php echo welcome_user(); ?>Code language: PHP (php)

Here’s the complete code:

<?php

function welcome_user($username) 
{ 
    return 'Welcome '. $username . '!';
}

$welcome_message = welcome_user('Admin');

echo $welcome_message;Code language: PHP (php)

HTML code inside the function #

Typically, a function contains only PHP code. However, it’s possible to define a function that contains HTML code. The following welcome() function displays the welcome message wrapped in a span tag:

<?php function welcome_user($username) { ?>
	<span>Welcome <?= $username ?></span>
<?php } ?>	Code language: PHP (php)

Summary #

  • A function is a named block of code that performs a specific task.
  • Do use functions to create reusable code.
  • Use the return statement to return a value from a function.
Did you find this tutorial useful?