Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
An optional parameter in PHP is a function or method parameter initialized with a default value in the declaration signature. If the caller omits the corresponding argument during invocation, the PHP engine automatically binds the parameter to its default value, preventing an ArgumentCountError.
Syntax
Optional parameters are defined using the assignment operator (=) directly within the parameter list.
function executeProcess(string $requiredParam, int $optionalParam = 100): void {
// Implementation
}
Lexical Rules and Constraints
1. Parameter Ordering
Optional parameters must be positioned sequentially at the end of the parameter list. Declaring a required parameter after an optional parameter breaks positional argument resolution and, as of PHP 8.0, triggers a Deprecated notice.
// Valid: Optional parameters at the end
function configure(string $host, int $port = 80, bool $secure = false) {}
// Deprecated (PHP 8.0+): Required parameter follows optional parameter
function configure(string $host = 'localhost', int $port) {}
2. Constant Expression Requirement
The default value must evaluate to a constant expression at compile time. It cannot be a variable, a class property, or a standard function call. Allowed types include scalars (integers, floats, strings, booleans), arrays, and null.
// Invalid: Cannot use a function call or variable as a default
function setTimestamp(int $time = time()) {}
// Valid: Using a constant
define('DEFAULT_TIME', 3600);
function setTimestamp(int $time = DEFAULT_TIME) {}
3. Object Initializers (PHP 8.1+)
Starting with PHP 8.1, the new keyword can be used within parameter declarations to instantiate objects as default values.
class DefaultLogger {}
function initializeLogger(object $logger = new DefaultLogger()): void {
// Implementation
}
Type System Interactions
Implicit vs. Explicit Nullability
Assigning null as a default value historically made a parameter implicitly nullable. Modern PHP strictly requires explicit nullability using the ? prefix or union types to maintain strict type safety and avoid deprecation warnings in newer versions.
// Legacy implicit nullability (Deprecated in PHP 8.4)
function setCache(string $key = null) {}
// Modern explicit nullability (PHP 7.1+)
function setCache(?string $key = null) {}
// Union type nullability (PHP 8.0+)
function setCache(string|null $key = null) {}
Named Arguments (PHP 8.0+)
With the introduction of named arguments, the strict positional requirement during invocation is bypassed. Developers can skip optional parameters regardless of their position in the signature by explicitly targeting the parameter names.
function buildQuery(string $table, int $limit = 10, string $order = 'ASC') {}
// Skipping the optional $limit parameter by using named arguments
buildQuery(table: 'users', order: 'DESC');
Master PHP with Deep Grasping Methodology!Learn More