Function Arguments
Arguments are how you feed data into a function. PHP gives you a lot of control over what shape that data is allowed to take: plain positional values, typed parameters that reject the wrong kind of data, or entire arrays and objects passed in as a single argument. Getting comfortable with argument handling — and with what PHP does when a call doesn't match a function's expectations — is essential for writing functions that fail loudly and predictably instead of quietly doing the wrong thing.
Positional parameters
By default, arguments are matched to parameters by their position in the call, left to right. The first argument fills the first parameter, the second fills the second, and so on.
Positional arguments
<?php
function makeFullName($first, $last) {
return "{$first} {$last}";
}
echo makeFullName("Ada", "Lovelace");Ada Lovelace
Adding type declarations
You can (and should) declare the expected type of each parameter. This is called a type declaration (sometimes still called a "type hint" from PHP 5-era terminology). If a caller passes a value of the wrong type, PHP will attempt to coerce it in weak-typing mode, or throw a TypeError if it can't be coerced sensibly — and if the file starts with declare(strict_types=1);, no coercion is attempted at all.
Typed parameters
<?php
function greet(string $name, int $age) {
return "{$name} is {$age} years old.";
}
echo greet("Tomás", 34);
echo "\n";
// Weak typing coerces this numeric string to an int
echo greet("Zoë", "34");Tomás is 34 years old. Zoë is 34 years old.
Passing arrays and objects
Arrays and objects are passed to functions just like any other value — as a single argument. Arrays are passed by value (the function gets its own copy, so modifying it inside the function does not affect the caller's array), while objects are passed by handle (the function gets a reference to the same object, so mutating its properties does affect the original).
An array argument and an object argument
<?php
function totalPrice(array $prices) {
return array_sum($prices);
}
echo totalPrice([9.99, 4.50, 2.25]);
echo "\n";
class Cart {
public array $items = [];
}
function addItem(Cart $cart, string $item) {
$cart->items[] = $item;
}
$cart = new Cart();
addItem($cart, "Keyboard");
print_r($cart->items);16.74
Array
(
[0] => Keyboard
)Too few arguments: ArgumentCountError
In PHP 8, calling a function without enough arguments to satisfy its required (non-default) parameters is a hard error, not a silent null. PHP throws an ArgumentCountError, which — unlike PHP 7's mere warning — will stop execution unless you catch it.
Missing a required argument
<?php
function divide($numerator, $denominator) {
return $numerator / $denominator;
}
echo divide(10);PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function divide(), 1 passed and exactly 2 expected
Extra arguments are ignored (unless captured)
The opposite situation — passing more arguments than a function declares — is not an error at all. PHP simply ignores anything beyond the declared parameters unless the function explicitly captures the rest with the variadic ...$args syntax.
Extra positional arguments are silently dropped
<?php
function add($a, $b) {
return $a + $b;
}
echo add(2, 3, 999, "ignored");5