PHP Function Parameters (Step-by-Step Guide with Examples)
In PHP, function parameters allow you to pass values to functions and control their behavior. Parameters make functions more dynamic and reusable by modifying their output based on input values.
1. What Are Function Parameters in PHP?
Parameters are variables listed inside the function parentheses ()`. When calling the function, you provide arguments (actual values) that are assigned to these parameters.
Syntax of a Function with Parameters
function functionName($param1, $param2) {
// Function code
}
2. Passing Parameters to PHP Functions
Example: Function with One Parameter
<?php
function greetUser($name) {
echo "Hello, $name!";
}
greetUser("John"); // Calling function with argument
?>
Output:
Hello, John!
Example: Function with Multiple Parameters
<?php
function addNumbers($a, $b) {
$sum = $a + $b;
echo "Sum: $sum";
}
addNumbers(5, 10);
?>
Output:
Sum: 15
3. PHP Function Parameters with Default Values
If an argument is not provided, default values are used.
Example: Default Parameter
<?php
function welcome($name = "Guest") {
echo "Welcome, $name!";
}
welcome(); // Uses default value
welcome("Alice"); // Overrides default value
?>
Output:
Welcome, Guest!
Welcome, Alice!
4. Passing Arguments by Value (Default Behavior)
By default, PHP passes arguments by value, meaning the original variable remains unchanged.
Example: Pass by Value
<?php
function increase($num) {
$num++;
echo "Inside Function: $num";
}
$number = 5;
increase($number);
echo "Outside Function: $number";
?>
Output:
Inside Function: 6 Outside Function: 5
➡ The $number
remains unchanged outside the function.
5. Passing Arguments by Reference (Using &)
To modify the original variable inside a function, use &
(pass by reference).
Example: Pass by Reference
<?php
function increment(&$num) {
$num++;
}
$number = 10;
increment($number);
echo "New Value: $number"; // The original variable is modified
?>
Output:
New Value: 11
➡ The $number
changes globally because we passed it by reference.
6. Using func_get_args() to Accept Unlimited Arguments
PHP allows functions to accept an unlimited number of arguments using func_get_args()
.
Example: Function with Dynamic Parameters
<?php
function sumAll() {
$args = func_get_args(); // Get all arguments as an array
$sum = array_sum($args); // Calculate sum
echo "Total Sum: $sum";
}
sumAll(5, 10, 15, 20);
?>
Output:
Total Sum: 50
7. Using ... (Variadic Functions) for Multiple Arguments
PHP 5.6+ supports variadic functions using ...
(spread operator) to accept any number of arguments.
Example: Variadic Function in PHP
<?php
function multiplyAll(...$numbers) {
$product = array_product($numbers);
echo "Product: $product";
}
multiplyAll(2, 3, 4); // 2 * 3 * 4 = 24
?>
Output:
Product: 24
8. Returning Values from PHP Functions
Functions can return values using the return
statement.
Example: Function with Return Value
<?php
function multiply($a, $b) {
return $a * $b;
}
$result = multiply(4, 5);
echo "Multiplication Result: $result";
?>
Output:
Multiplication Result: 20
➡ Using return
allows you to store and use the result later.
9. PHP Function Parameters with Type Hints (PHP 7+)
PHP allows type hinting to enforce parameter data types.
Example: Type Hinting for Integers
<?php
function addNumbers(int $a, int $b): int {
return $a + $b;
}
echo addNumbers(10, 20);
?>
Output:
30
➡ This ensures only integers are accepted.
10. PHP Function Parameters with Arrays and Objects
You can pass arrays and objects as parameters.
Example: Passing an Array
<?php
function sumArray($numbers) {
return array_sum($numbers);
}
$values = [10, 20, 30];
echo "Total: " . sumArray($values);
?>
Output:
Total: 60
11. Best Practices for Function Parameters in PHP
✅ Use meaningful parameter names
✅ Set default values where possible
✅ Use type hints (e.g., int
, string
, array
) for better validation
✅ Use pass-by-reference (&
) when modifying values inside the function
✅ Keep functions short and focused