How To Create User-Defined Functions In PHP

How To Create User-Defined Functions In PHP

·

4 min read

In this tutorial, we will be learning how to create your own (user-defined) functions.

What Is A Function?

For all programming languages, a function is a block of statements that you can repeatedly use in your program; PHP is no exception to this.

PHP, just like any programming language, comes with many built-in functions. It also (not surprisingly) has a syntax which allows you to create your own functions.

Built-In Functions

PHP comes with more than one thousand built-in functions. These are functions that PHP has already created for you by default.

You do not have to memorize them individually. Use them enough, look them up as you need them, and you will eventually understand and remember the ones you need.

Make Your Own Function

You are also free to design your own function; we will call these “user-defined functions”.

Creating A User-Defined Function

Here is the syntax of a user-defined function:

Syntax

<?php
function functionname
{
Statement 1; 
Statement 2; 
Statement 3; 
Statement 4; 
}
?>

What happens is whenever you write functionname(), statement 1, statement 2, statement 3 and statement 4 will all execute in that order. Functions do not automatically execute on their own when the script loads, you must call the function each time you want the block of code to execute. In these words, you must do this to make functionname() execute

<?php
function function_name()
{}
.....
function_name();
?>

Elements of A Function

All functions begin with the keyword function. After the keyword, you specify the name of your function.

You can name your function whatever you want, so as long as it is not a reserved keyword. However, generally you want to pick a name that describes what the function does. The name of your function can only be one word. For example, you cannot the code below will not work

<?php
function function name()
{
}
?>

The space between function and name will make the code above not executable. If you want to include multiple words in your name, you should separate them using an underscore(_).

Case Sensitivity

One thing to remember for PHP is that function names are case-insensitive. Function_name, FUNction_NAme, FUNCTION_NAME and Function_Name will execute the same thing.

Example: To call a function

<?php
Function my_function()
{
Echo “Hello World”;
}
my_function();
my_Function();
mY_FUNction();
?>

Result 1.png The above code will execute my_function() 3 times.

How PHP Functions Take Arguments

While not mandatory to include, one of the function’s main purpose is to take in some arguments and then manipulate them in some way. Example

<?php
function add($a, $b)
{
return $a+$b;
}
echo add(10, 20);
?>

Result

2.png In the function above, we take in two arguments. And we then add the two parameters together. There is no limit to how many arguments a function can take: however, you must input the correct number of arguments every time you execute a function! If for example a function requires two arguments, and you only add one(or you add three), you will produce an error.

The Return Keyword

While you can write very complex and beautiful functions, at the end of the day, a function can really only do one of two things. It can execute code only, or it can execute some code and then return a value.

Example: A Function That Does Not Return Anything

<?php
function display_me($a)
{
echo $a;
}
display_me("hello world");
?>

Results

3.png The function I wrote above doesn’t return anything. It only takes a parameter and then prints out whatever text has been entered. However, a function can also have a return statement that returns a value. This means the function will do whatever manipulation it does and then finally return a particular value with a return statement. This return statement should be the last line of the code block in your function. You use the return keyword to return the value. And after the return statement, your function terminates immediately. Example: In the following statement, a function accepts a number and calculates the areas of a rectangle with those sides and then returns the area.

<?php
function rec_area($height, $width)
{
return $height*$width;

}
echo rec_area(10,20);
?>

Result 4.png You are allowed, by the way, to return any type of data you want.