PHP 8 Named Arguments

In PHP 8, you can use named arguments, which allows you to provide the parameters of a function in any order:

PHP 7.x

// haystack must come first
$pos = strpos('Hello World', 'World');

PHP 8

// I can tell it what each param is, so the order is up to me
$pos = strpos(needle: 'World', haystack: 'Hello World');

Even if you choose not to reorder the params, it will make the code clearer and easier to read.

Leave a Reply

Your email address will not be published. Required fields are marked *