PHP Functions
A function in PHP is a block of reusable code that performs a specific task. Functions help in organizing code, improving reusability, and reducing redundancy. PHP has both built-in functions (like strlen()
, array_push()
, etc.) and user-defined functions.
1. Defining and Calling a Function
Syntax:
Example:
Output:
2. Function with Parameters
You can pass parameters (also called arguments) to functions.
Example:
Output:
3. Function with Return Value
Functions can return values using the return
statement.
Example:
Output:
4. Default Parameter Values
You can set a default value for parameters.
Example:
Output:
5. Function with Multiple Parameters
A function can accept multiple parameters.
Example:
Output:
6. Passing Arguments by Reference
By default, function parameters are passed by value. If you want to modify the original variable, use pass by reference with the &
symbol.
Example:
Output:
7. Variable-Length Arguments (...
Operator)
PHP allows functions to accept a variable number of arguments using the ...
operator (also called variadic functions).
Example:
Output:
8. Anonymous Functions (Lambda/Closure)
PHP allows defining functions without names, known as anonymous functions or closures.
Example:
Output:
9. Arrow Functions (PHP 7.4+)
Arrow functions are a shorter way to define anonymous functions.
Example:
Output:
10. Recursive Functions
A recursive function is a function that calls itself.
Example: Factorial Calculation
Output:
5! = 5 × 4 × 3 × 2 × 1 = 120
11. Built-in PHP Functions
PHP provides many built-in functions for different tasks.
String Functions
strlen($str)
→ Returns the length of a string.strtoupper($str)
→ Converts string to uppercase.strtolower($str)
→ Converts string to lowercase.strpos($str, "word")
→ Finds the position of a word in a string.str_replace("old", "new", $str)
→ Replaces text in a string.
Example:
Array Functions
count($array)
→ Returns the number of elements in an array.array_push($array, $value)
→ Adds an element to the end of an array.array_pop($array)
→ Removes the last element of an array.sort($array)
→ Sorts an indexed array in ascending order.
Example:
Output:
12. Function Overriding (Not Supported in PHP)
Unlike some programming languages, PHP does not support function overloading (i.e., defining multiple functions with the same name but different parameters). However, you can achieve similar behavior using default parameters or variadic functions.
Example (Simulating Function Overloading with ...
Operator)
Output:
13. Global vs Local Variables in Functions
Local Variable
A variable declared inside a function is local and cannot be accessed outside.
Example:
Global Variable
A global variable is declared outside a function and can be accessed using the global
keyword.
Example:
Output:
Conclusion
- PHP functions organize code and improve reusability.
- Functions can accept parameters and return values.
- You can define anonymous functions and arrow functions for shorter code.
- PHP provides built-in functions for strings, arrays, and math operations.
- Recursion allows functions to call themselves for complex calculations.