← Back to PHP Course | Chapter 5: Functions | Lesson 2 of 10

PHP Function Parameters

Parameters are like the blanks on a recipe that you fill in each time, such as how many people you are cooking for. The function uses whatever you hand it when you call it.
Syntax
php
function function_name($parameter1, $parameter2) {
    // body
}

function function_name(&$by_reference) { }   // pass by reference

function_name($argument1, $argument2);

What are Parameters?

Parameters are the named placeholders listed inside a function's parentheses when you define it.

When you later *call* the function, the arguments you supply fill those placeholders in order, giving the function's body something concrete to work with.

Example: What are Parameters?

php
<?php
// Define the function `greet` taking `$name`
function greet($name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Pass by Value

By default, PHP copies an argument's value into the function — any changes made to the parameter inside the function body stay local and never touch the original variable back where the function was called.

This is why plain function calls can't accidentally mutate a variable you pass in.

Example: Pass by Value

php
<?php
// Define the function `addOne` taking `$num`
function addOne($num) {
    // Declare `$num`, set to `$num + 1`
    $num = $num + 1;
    // Print "Inside: $num\n" to the output
    echo "Inside: $num\n";
}
// Declare `$x`, set to `5`
$x = 5;
// Call `addOne($x)`
addOne($x);
// Print "Outside: $x" to the output
echo "Outside: $x";
?>

Pass by Reference

Prefixing a parameter name with & in the function definition switches it to pass-by-reference: the function now operates on the original variable itself, so any change it makes is visible back at the call site once the function returns — the opposite of the default copy behavior.

Example: Pass by Reference

php
<?php
// Define the function `addOne` taking `&$num`
function addOne(&$num) {
    // Declare `$num`, set to `$num + 1`
    $num = $num + 1;
}
// Declare `$x`, set to `5`
$x = 5;
// Call `addOne($x)`
addOne($x);
// Print `$x` to the output
echo $x;
?>

Typed Parameters

Adding a type declaration before a parameter name (function greet(string $name)) tells PHP to enforce that type at the call site.

Pass something that doesn't match — an array where a string was expected — and PHP throws a TypeError immediately, rather than silently coercing or misbehaving later.

Example: Typed Parameters

php
<?php
// Define the function `greet` taking `string $name`
function greet(string $name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Variadic Parameters (... operator)

The ... operator before a parameter name collects any number of extra arguments into a single array inside the function, letting you write one function that accepts a flexible, unknown-in-advance number of values rather than a fixed parameter list.

Example: Variadic Parameters (... operator)

php
<?php
// Define the function `sumAll` taking `...$numbers`
function sumAll(...$numbers) {
    // Return `array_sum($numbers)` from this function
    return array_sum($numbers);
}
// Print `sumAll(1, 2, 3, 4)` to the output
echo sumAll(1, 2, 3, 4);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a change to a parameter to affect the caller's variable, when arguments are passed by value unless declared with &.
  2. Passing too few arguments, which throws an ArgumentCountError in PHP 7.1 and later.
  3. Passing a string like "abc" to an int parameter, which throws a TypeError under declare(strict_types=1) and is coerced or fails otherwise.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.