Created
March 10, 2023 05:20
-
-
Save prashantdsala/424fa33f3461eb98f2e3e2107d16f4b6 to your computer and use it in GitHub Desktop.
Named arguments in PHP 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Named arguments were introduced in PHP 8, | |
// and they allow you to pass arguments to a function or method by specifying the name of the argument | |
// rather than relying on the order in which the arguments are defined. | |
// Here's an example of using named arguments in PHP: | |
function createUser($name, $email, $age) { | |
echo "Name: $name <br>"; | |
echo "Email: $email <br>"; | |
echo "Age: $age <br>"; | |
} | |
// Using named arguments | |
createUser(age: 25, name: 'John Doe', email: 'johndoe@example.com'); | |
// In the above example, we have a function called createUser() that takes three arguments: name, email, and age. | |
// We call the function using named arguments, which allows us to specify the values of the arguments by name instead of position. | |
// Using named arguments can make code more readable, | |
// especially when there are many arguments or when the order of the arguments is not immediately clear. | |
// It can also make it easier to add new arguments to a function without having to worry about breaking existing code that relies on argument order. | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment