- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Named Arguments in PHP 8
In PHP 7, we used to have positional parameters. That means, we need to assign the position of the first argument to the first parameter only. The default value is used for any missing arguments in PHP 7.x versions.
In PHP 8, we can pass the arguments to a function based on the parameter name, rather than passing the parameter position. Order doesn’t matter in PHP 8, it is allowed to skip default values randomly and it is also self-documenting.
Example − Named Arguments in PHP 8
In PHP 8, arguments are order-independent and self-documented.
We can skip the optional parameters but specify only the required parameters.
<?php function sample($num = 1, $value = 5){ echo "Number: ", $num; echo " "; echo "Value: ", $value; } sample(value: 5, num: 30); //Named arguments in different order ?>
Output
Number: 30 Value: 5
In the function definition, argument names are matched with the parameter names. So, this code runs without any error.
- Related Articles
- Named arguments in JavaScript.
- Named Arguments in Lua Programming
- How to use named arguments in JavaScript functions?
- PHP Function arguments
- Passing static methods as arguments in PHP
- Pass arguments from array in PHP to constructor
- Attributes in PHP 8
- Number comparisons in PHP 8
- Union Type in PHP 8
- Match Expression in PHP 8
- Nullsafe Operator in PHP 8
- fdiv() function in PHP 8
- Difference between gettype() in PHP and get_debug_type() in PHP 8
- Mixed Pseudo Type in PHP 8
- Constructor Property Promotion in PHP 8

Advertisements