DEV Community

Cover image for PHP 8 News: Named Arguments
Antonio Silva
Antonio Silva

Posted on

1

PHP 8 News: Named Arguments

Named arguments in PHP 8 allow you to pass arguments to a function by specifying the parameter name along with the value, rather than relying on the order of parameters as defined in the function signature. This feature provides more flexibility and readability, especially for functions with a large number of parameters or optional parameters.

Here's how named arguments work in PHP 8:

Passing Arguments by Name

Instead of relying on the order of parameters, you can specify the parameter name followed by the value you want to pass.

function greet(string $name, int $age): void {
    echo "Hello, $name! You are $age years old.";
}

// Without named arguments
greet('Zero', 24); // Outputs: Hello, Zero! You are 24 years old.

// With named arguments
greet(name: 'Zero', age: 24); // Outputs: Hello, Zero! You are 24 years old.
Enter fullscreen mode Exit fullscreen mode

Skipping Optional Parameters

When a function has optional parameters, you can skip them and only provide values for the parameters you want to set.

function greet(string $name, int $age = 18): void {
    echo "Hello, $name! You are $age years old.";
}

// Without named arguments
greet('Zero'); // Outputs: Hello, Zero! You are 18 years old.

// With named arguments
greet(name: 'Zero'); // Outputs: Hello, Zero! You are 18 years old.
Enter fullscreen mode Exit fullscreen mode

Non-Sequential Parameter Passing

Named arguments allow you to pass parameters in any order, making the code more readable and reducing the chances of errors, especially when functions have many parameters.

function greet(string $name, int $age): void {
    echo "Hello, $name! You are $age years old.";
}

// Using named arguments in any order
greet(age: 24, name: 'Zero'); // Outputs: Hello, Zero! You are 24 years old.
Enter fullscreen mode Exit fullscreen mode

Named arguments improve code readability and make it easier to understand the intent of the function call, especially when dealing with functions that have multiple parameters or default values. However, it's essential to use named arguments judiciously and consider readability and maintainability when deciding whether to use them.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay