Using Default Arguments in a Function

Using Default Arguments in a Function

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Positional Nature of Default Arguments in PHP

When defining a function like foo($blah, $x = "some value", $y = "some other value"), it is crucial to understand how PHP maps arguments to parameters. By default, PHP uses positional mapping. This means the first argument you pass fills the first parameter, the second fills the second, and so on. Default values act as placeholders only when a specific position in that sequence is left empty.

If you attempt to call foo("blah", null, "test"), you are explicitly providing an argument for every single parameter. The null value replaces the default string "some value" for $x. Similarly, passing an empty string "" also occupies the slot for $x. In both cases, the function receives exactly three arguments: one for $blah, one for $x, and one for $y. Consequently, the logic inside your function will execute using these explicit values rather than the defaults defined in the signature.

Attempting to use named arguments like foo("blah", $x, $y = "test") does not work as an intuitive workaround because PHP has historically ignored this syntax for standard functions. While modern languages like Python or JavaScript allow you to skip a positional argument and define a subsequent one by name (e.g., func(arg1, arg3=5)), PHP treats the function signature strictly based on order unless specific conditions are met. If you write foo("blah", $x, $y = "test"), PHP sees three distinct arguments being passed in positions 1, 2, and 3. The assignment to $y inside the call is simply ignored or treated as a syntax error depending on context; it does not skip the second parameter.

To achieve the desired behavior where $x uses its default while $y receives a custom value, you must omit the argument entirely for the position you wish to skip. The only way to trigger the default value for $x is to pass exactly two arguments:

foo("blah", "test"); 
// Here: $blah = "blah", $x = "test", $y = "some other value" (default)

If your goal is to pass a custom value to $y but skip $x, you must explicitly pass the default value for $x or use a variable that holds that default. You cannot simply leave a gap in the middle of the argument list. This constraint often surprises developers coming from languages with more flexible optional parameter syntax.

There is, however, a way to bypass this limitation by reordering the parameters in your function definition so that the required arguments come after the optional ones. If you define $y before $x, then foo("blah", "test") would map "blah" to $y and "test" to $x. This is often considered bad practice, though, because it forces callers to pass values in an order that doesn't match logical grouping.

If you are working within the Laravel ecosystem, you might encounter similar concepts when interacting with service providers or facades where optional dependencies are injected. Laravel's container often utilizes type hinting and default values extensively. While Laravel does not support skipping positional arguments mid-function call natively in standard PHP functions, understanding this limitation helps prevent subtle bugs when writing custom logic for Eloquent models or artisan commands.

For scenarios requiring true named argument flexibility without reordering parameters, consider wrapping the function in a closure or using a class method where you can explicitly control which properties are set. Alternatively, switch to PHP 8.0+ features like union types or pattern matching if your codebase permits it, though these do not fundamentally change how positional defaults work in standard procedural functions. The core takeaway remains: in standard PHP function calls, position dictates mapping, and default values only activate when that position is truly empty.

Our network