PHP Function

Created with Sketch.

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: HTML, XML (xml)

If you want to show the same message in other places, you must copy the above statement and paste it 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: HTML, XML (xml)

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

<?php

welcome();

Code language: HTML, XML (xml)

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

By 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: HTML, XML (xml)

In this syntax:

  • First, specify the function name followed by the function keyword. The name of the function needs to start with a letter or underscore followed by zero or more letters, underscore, 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: HTML, XML (xml)

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. The inputs make functions reusable and more useful. And the inputs of a function 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: HTML, XML (xml)

Inside the function body, you can use the parameters like variables. In fact, 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: HTML, XML (xml)

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: HTML, XML (xml)

For example:

<?php

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

welcome();

Code language: HTML, XML (xml)

The welcome() function shows the following message:

Welcome!

 

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('Admin');

Code language: HTML, XML (xml)

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

Welcome Admin!

 

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

welcome_user('Guest');

Code language: HTML, XML (xml)

Output:

Welcome Guest!

 

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: HTML, XML (xml)

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

welcome_user('Admin');

Code language: HTML, XML (xml)

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: JavaScript (javascript)

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: HTML, XML (xml)

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: HTML, XML (xml)

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: HTML, XML (xml)

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.

Leave a Reply

Your email address will not be published. Required fields are marked *