QA
Intermediate LevelPHP 8+

How do PHP 8.0 Named Arguments (func(param: $val)) improve readability and allow skipping optional parameters?

❓ Interview Prompt: Explain PHP 8.0 Named Arguments syntax, skipping default parameter positions, and combining with positional arguments.

Historically, calling a function with many optional parameters required passing positional arguments for every preceding default parameter.

Named Arguments (PHP 8.0+):

Pass arguments by parameter name using the 'paramName: $value' syntax:

1
Self-Documenting Code: Parameter names clarify purpose at call site.
2
Skip Default Parameters: Pass specific optional parameters without supplying dummy values for preceding parameters!
3
Order Independence: Named arguments can be passed in any order.

Code Implementation Example

php
<?php
// Setting cookie using Named Arguments (skipping path, domain, etc.)
setcookie(
    name: 'session_token',
    value: 'abc123xyz',
    expires: time() + 3600,
    secure: true,
    httponly: true,
    samesite: 'Strict'
);
💡Using PHP 8.0 Named Arguments to set secure cookies without positional boilerplate.
🎯 Interview Key Takeaway

Named Arguments (func(param: $val)) allow passing parameters by name, improving clarity and allowing skipping default positional arguments.

#Named Arguments#PHP 8.0#Functions#Intermediate
Back to all PHP 8+ Questions→

More PHP 8+ Interview Questions

Beginner LevelPHP

What is PHP and how does its shared-nothing request lifecycle work?

Beginner LevelPHP

What is the difference between echo and print in PHP?

Beginner LevelPHP

What is the difference between isset(), empty(), and is_null() in PHP?