Functions, Default Arguments, and Named Arguments
Declaring functions with typed parameters, default values, and PHP 8 named arguments.
What you'll learn
- Declare a function with typed parameters, a default value, and a return type
- Call a function using PHP 8's named-argument syntax
- Predict the output of functions called with a mix of positional, default, and named arguments
Explanation
A PHP function is declared with function, optional type hints on each parameter, and an optional return type: function greet(string $name, string $greeting = "Hello"): string { return "$greeting, $name!"; }. A parameter with = value after its type is a default argument -- callers may omit it entirely, in which case the default is used.
PHP 8 added named arguments: instead of matching parameters purely by position, you can call greet(name: "Linus", greeting: "Hey"), naming each parameter explicitly. This is especially useful for functions with several optional parameters -- you can supply just the ones you care about, by name, in any order, without needing placeholder values for the ones in between.
Named and positional arguments can be mixed in one call, as long as every positional argument comes before any named one: greet("Ada", greeting: "Hi") is valid; greet(greeting: "Hi", "Ada") is not.
Type hints on parameters are checked at call time (and coerced for scalar types, unless the file starts with declare(strict_types=1);) -- passing an incompatible type that can't be coerced raises a TypeError, which is one of several places modern PHP uses real exceptions instead of a silent warning.
Guided lab
Predict: Default and named arguments
Read this script and predict exactly what it sends to the browser.
<?php
function greet(string $name, string $greeting = "Hello"): string {
return "$greeting, $name!";
}
echo greet("Ada") . "\n";
echo greet("Grace", "Hi") . "\n";
echo greet(name: "Linus", greeting: "Hey") . "\n";Stuck? Get a hint.
Common mistakes
- Putting a named argument before a positional one in the same call -- PHP requires all positional arguments to come first.
- Assuming a default-argument parameter can be skipped by leaving a gap (like an empty comma) -- you either omit trailing optional arguments entirely or use named arguments to skip to a later one.
- Forgetting that scalar type hints are coerced by default (an int passed to a `string` parameter becomes a string) unless `declare(strict_types=1);` is active.
Knowledge check
Takeaway
Use default arguments to make parameters optional, and named arguments when you want to target a specific optional parameter without supplying every one before it.
Summary
Functions declare typed parameters and return types; default arguments make parameters optional; PHP 8 named arguments let callers target parameters by name instead of position.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.