PHP | Functions (original) (raw)

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.

Creating a Function in PHP

A function is declared by using the function keyword, followed by the name of the function, parentheses (possibly containing parameters), and a block of code enclosed in curly braces.

**Syntax

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

**In this syntax:

Calling a Function in PHP

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

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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 reference, the function works with a copy of the argument. This means the original value remains unchanged.

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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.

**Now, let us understand with the help of example:

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

Conclusion

PHP functions are essential building blocks for creating reusable and efficient code. By utilizing built-in functions and defining your own, you can make your code modular, easier to maintain, and more readable. Functions also help with data handling, performance improvement, and making your code more flexible.