简体   繁体   中英

Using “named” parameters in function call

Unfortunately PHP does not support named parameters, but I've noticed that the PHP syntax allows us something similar:

function list_items( $user, $archived ) { ... }

// This is the default way to call the function.
// It's not very clear what the value and false refer to:
list_items( 10, false );

// Alternative syntax I found which makes this more clear:
list_items( $user = 10, $archived = false );

Both function calls return the same data.

Question

  • Any information on the actual difference between both calls?
  • Is it a good idea to use the second kind of syntax or will it cause issues?
  • Is that second syntax supported in any PHP 5.x version?

To answer this question: There are no named parameters.

The example in my question simply assigns values to variables and then passes those variables to the function.

list_items( $user = 10, $archived = false );

// is same as this:
$user = 10;
$archived = false;
list_items( $user, $archived );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM