How to create and call PHP functions with examples

Introduction to function in PHP

Just like in other programming languages, PHP provides you the way to create and call functions.

A PHP function is a block of code that may take one or more input parameters, executes and may return a value. You can use functions repeatedly in PHP programs. A function’s code will only be executed once that function is called.

For example, built-in date and time functions in PHP like mktime(), MySQL function to establish a connection with database of MySQL (mysql_connect).

When you call these functions, you just know which parameters to pass but don’t know what the code is written to perform the ‘function’.

See an example of a function

There are plenty of built-in functions, however, programmers can create their own custom functions. The functions make the life of programmer quite easier in that you don’t have to write the code again and again. You just need to understand what the purpose of a function, what are the required parameters and return values.

You even don’t need to go through the code, if a function is written by someone else, like built-in functions or by other programmers.

Parts of PHP functions

There are two parts of a function:

  1. Creating a function
  2. Calling a function

Syntax of creating a function

You have to write the keyword function followed by the function_name(). All your code to be executed comes under the curly braces.

An example of creating a function without a parameter

Following example shows how to create a function. The is just a basic example that will execute statements and takes no input parameter.

a simple function

Experience this example online


This function will execute without needing a parameter

Creating a function with parameters

Now we will go through a simple example of creating a PHP function that takes two parameters as the input and displays the sum.

Experience this example online



In above example, when a function is called, the main program sends two variable values, $x and $y that are assigned to function’s parameters. The $x to $a and $y to $b parameter.

Function return example

Now we will go through an example of creating a function, that takes two parameters as input and the sum as the return value by function.

Function example with parameters and return value

Experience this example online



In the above example, a function is created with two required parameters. While it returns a value by using the return command. When we call it from the PHP program, we sent two variable values ($x,$y) and assigned to another variable of the same type to receive the returned value and finally displayed the output.

Function Optional Arguments Or Default Argument Value

It is possible to create a function with argument’s default values. In that case, while calling a function it becomes an optional parameter. While function will use the default value.

PHP function default

Experience this example online



As we assigned the default values while defining our function to its parameters, those parameters became optional i.e. when you call it from PHP program, it is not mandatory to send parameters and in that case, the function uses default values without generating an error.