PHP | Functions (original) (raw)

Last Updated : 28 Nov, 2025

A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value.

result=result = result=a + $b; // returning the sum return $result; } // Calling the function echo addNumbers(5, 10); ?>

`

**Syntax

function functionName($param1, $param2) {
// Code to be executed
return $result; // optional }

Calling a Function in PHP

Once a function is declared, it can be invoked (called) by simply using the function name followed by parentheses.

PHP `

a+a + a+b; } echo sum(5, 3); // Outputs: 8 ?>

`

**In this example:

Types of Functions in PHP

PHP has two types of functions:

1. User-Defined Functions

A user-defined function is created to perform a specific task as per the developer's need. These functions can accept parameters, perform computations, and return results.

PHP `

a+a + a+b; } echo addNumbers(5, 3); // Output: 8 ?>

`

**In this example:

2. Built-in Functions

PHP comes with many built-in functions that can be directly used in your code. For example, strlen(), substr(), array_merge(), date() and etc are built-in PHP functions. These functions provide useful functionalities, such as string manipulation, date handling, and array operations, without the need to write complex logic from scratch.

PHP `

`

**In this example:

PHP Function Parameters and Arguments

Functions can accept input values, known as parameters or arguments.

These values can be passed to the function in two ways:

1. Passing by Value

When you pass a value by value, the function works with a copy of the argument, so the original value remains unchanged.

PHP `

x=x = x=x + $y; return $x; } echo add(2, 3); // Outputs: 5 ?>

`

**In this example:

2. Passing by Reference

By passing an argument by reference, any changes made inside the function will affect the original variable outside the function.

PHP `

x=x = x=x + $y; } $a = 5; addByRef($a, 3); echo $a; // Outputs: 8 ?>

`

**In this example:

Returning Values in PHP Functions

Functions in PHP can return a value using the return statement. The return value can be any data type such as a string, integer, array, or object. If no return statement is provided, the function returns null by default.

PHP `

a∗a * ab; } result=multiply(4,5);//result = multiply(4, 5); // result=multiply(4,5);//result will be 20 echo $result; // Output: 20 ?>

`

**In this example:

Anonymous Functions (Closures)

PHP supports anonymous functions, also known as closures. These functions do not have a name and are often used for passing functions as arguments to other functions.

PHP `

greet=function(greet = function(greet=function(name) { echo "Hello, " . $name . "!"; }; $greet("GFG"); ?>

`

**In this example:

Recursion in PHP

Recursion is a process in which a function calls itself. It is often used in situations where a task can be broken down into smaller, similar tasks.

PHP `

n∗factorial(n * factorial(nfactorial(n - 1); } } echo factorial(5); ?>

`

**In this example:

Common PHP Functions

Here are some common PHP functions you should know:

1. String Functions

To read about PHP String Functions refer this article - PHP String Functions

2. Array Functions

To read about PHP Array Functions refer this article - PHP Array Functions

3. Date and Time Functions

To read about PHP Date and Time Functions refer this article - PHP Date and Time Functions

Best Practices for Writing PHP Functions