
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP - Named Arguments
The feature of Named Arguments has been introduced in PHP with the version 8.0. It is an extension of the existing mechanism of passing positional arguments to a function while calling.
By default, values of passed arguments are copied to the corresponding formal arguments at the same position. This feature of named arguments in PHP makes it possible to pass the value based on the parameter name instead of the position.
Example with Positional Arguments
If we have a function defined as follows −
function myfunction($x, $y) { statement1; statement2; . . . }
and it is called as −
myfunction(10, 20);
In this case, the values are passed to the variables "x" and "y" in the order of declaration. It means, the first value to the first argument, second value to second argument and so on. The variables "x" and "y" are positional arguments.
To pass the values by named arguments, specify the parameter name to which argument the value is to be passed. The name of the parameter is the name of formal argument without the "$" symbol. The value to be passed is put in front of the ":" symbol.
myfunction(x:10, y:20);
Example with Named Arguments
Here is the code that demonstrates how you can use named arguments in PHP −
<?php function myfunction($x, $y) { echo "x = $x y = $y"; } myfunction(x:10, y:20); ?>
Output
It will produce the following output −
x = 10 y = 20
Using named arguments makes it possible to pass the values in any order, and not necessarily in the same order in which the arguments are declared in the function definition. We can call myfunction() as shown below and it will produce the same result.
myfunction(y:20, x:10);
With this feature, the arguments become order-independent and self-documenting. It also makes it possible to skip the arguments with default values arbitrarily.
Combining Named Arguments with Positional Arguments
Named arguments can be combined with positional arguments, with the condition that, the named arguments must come after the positional arguments.
<?php function myfunction($x, $y, $z) { echo "x = $x y = $y z = $z"; } myfunction(10, z:20, y:30); ?>
Output
It will generate the following result −
x = 10 y = 30 z = 20
However, if you try to treat $z as a positional argument,
myfunction(x:10, y:20, 30);
In this case, PHP will encounter the following error −
PHP Fatal error: Cannot use positional argument after named argument in hello.php on line 7
Passing Named Arguments from an Array
PHP 8.1.0 also introduced another feature that allows using named argument after unpacking the arguments. Instead of providing values to each argument individually, the values in an array an be unpacked into the corresponding arguments, using "..." (three dots) before the array.
<?php function myfunction($x, $y, $z=30) { echo "x = $x y = $y z = $z"; } myfunction(...[10, 20], z:30); ?>
Output
It will produce the following output −
x = 10 y = 20 z = 30
Note that passing the same parameter multiple times results in an exception as follows −
myfunction(x:10, z:20, x:20);
Error −
PHP Fatal error: Uncaught Error: Named parameter $x overwrites previous argument in hello.php:7
Positional Arguments vs Named Arguments
Positional arguments are passed in the same order as the function parameters. In this the position or order matters. See the example for positional argument −
<?php def greet(name, age): print(f"Hello, my name is {name} and I am {age} years old.") greet("Amit", 25) ?>
Output
It will produce the following output −
Hello, my name is Amit and I am 25 years old.
While the arguments passed with parameter names are called Named arguments. The position does not matter because you name the arguments.
<?php greet(name="Amit", age=25) ?>
Output
It will produce the following output −
Hello, my name is Amit and I am 25 years old.
Or change the order −
<?php greet(age=25, name="Amit") ?>
The output for the above code will be the same. So it proves if we change the order of arguments it does not affect the output of the program.
Passing Arguments by Reference
Normally, when you pass a variable to a function, PHP makes a copy of it. If you change a variable inside the function, the original value outside the function remains unchanged. But when you pass a reference (with the & symbol), the function uses the original variable rather than a copy. So, if the function changes the variable, it will be reflected outside of the function!
<?php function updateString(&$str) { $str .= " updated"; } $text = "This is just a test string"; updateString($text); echo $text; ?>
Output
It will generate the below output −
This is just a test string updated
It's useful when you want to update a variable within a function but maintain the change outside of the function. But you have be careful because falsely changing the variable can lead to unexpected outcomes in your program.