PHP is excellent for generating dynamic web pages and apps. PHP scripts are executed on the server to generate HTML content, which is then delivered to the client’s web browser.
PHP 8 is a major update of the PHP programming language that includes several upgrades, new features, and speed increases, such as the named arguments feature.
Named arguments allow us to send parameters to a function by giving the parameter name, making function calls easier to understand as they are not dependent on argument order. This improves code readability and makes it easier to maintain, particularly for functions with many arguments.
Here are some key aspects of the named arguments feature:
Named arguments in PHP 8 provide a more flexible way to pass arguments to functions.
Instead of relying on the order of parameters, we can specify the parameter name followed by the corresponding value.
This enhances code readability, especially for functions with many parameters or optional parameters.
In this PHP example, we have a function called greet
that takes three parameters: $name
, $age
, and an optional parameter $greeting
. Next, we will call this function with a random arrangement of input parameters, and the function will then print a message using these parameters. Let’s see what output we get.
<?phpfunction greet($name, $age, $greeting = "Hello") {echo "$greeting, $name! You are $age years old.";}// Normal function call in PHPgreet("Hi", "Zack", 25);?>
Let’s get into the code.
Lines 2–3: The greet
function takes three parameters: $name
, $age
, and $greeting
. The $name
and $age
parameter is intended for the person’s name and age, respectively. The $greeting
parameter has a default value of “Hello.’’ If a value is not provided when the function is called, it will use “Hello’’ as the default value. Inside the function, echo
is used to output a message that combines the greeting, the person’s name, and their age.
Line 7: Here, we call the greet
function with three arguments. The first argument, 25
, corresponds to the $name
parameter in the function. However, the order is incorrect. It should be the person’s name, not their age. The second argument, "Zack"
, corresponds to the $age
parameter in the function. The third argument, "Hi"
, corresponds to the $greeting
parameter in the function.
There are a couple of intricacies in the coding example above:
In the function call greet(25, "Zack", "Hi");
, the order of arguments seems to be incorrect. The function is defined with the parameters. $name
, $age
, and $greeting
, in that order. However, in the function call, the arguments are provided as 25
, "Zack"
, "Hi"
. It’s typically expected to pass arguments in the order in which they are defined in the function signature. Here’s the corrected function call:
greet("Zack", 25, "Hi");
The function greet
has a default value for the third parameter, $greeting
, set to “Hello.’’ If a value is not provided for $greeting
in the function call, it will default to “Hello.’’ In the function call above, we provided a value for $greeting
, but if you want to rely on the default value, you can also call the function like this:
greet("Zack", 25);
Now, let’s modify our code and use the named arguments in the function call. To add named arguments, all we need to do is specify the parameter names along with their values when calling the function. For example, instead of just passing "Hi", "Zack", 25
as our input arguments, we’ll pass greeting: "Hi", name: "Zack", age: 25
.
<?phpfunction greet($name, $age, $greeting = "Hello") {echo "$greeting, $name! You are $age years old.";}// Using named parameters in PHP 8greet(greeting: "Hi", name: "Zack", age: 25 );?>
In line 7, the function call still has an incorrect order of arguments, but the output will be Hi, Zack! You are 25 years old.
which is the correct sentence that we expect from this function. So, if we use named arguments, no matter the order of arguments, our function will assign values to the input arguments correctly.
Named arguments in PHP 8 significantly improve how parameters are passed to functions, making the code more readable and maintainable. By specifying parameters by name, developers can avoid mistakes related to argument order and improve the clarity of function calls, especially when dealing with functions with numerous or optional parameters. This feature is a valuable addition to PHP, enhancing the overall developer experience and improving code quality.
Free Resources