声明函数时PHP的参数顺序

This is a curiosity question. When creating a function, is there a recommended way to order the parameters? I mean, if I am going to create a function that receives 3 parameters, lets say int $a, $str b and $mixed $c, is there a reason to pick one order over another? like prefer function f($a, $b, $c) instead of function f($c, $b, $c) based on whatever? like the parameter types or anything else?

I know that optional parameters must always go at the end, but apart of that, is there any standard or practical recommendations that would affect performance or something?

In short, I only want to know if the order of the parameters of the function is always arbitrary or not.

NOTE: my question is about when functions are declared only.

They are relatively arbitrary, however there's a two main methodologies I've seen. The least common is "Order of Use", this only makes sense in sequential functions, where $this comes before $that.

However the typical way is something along the lines of "most important" or required arguments. You don't want important, optional, optional, optional, optional, optional, important - because then you'll need the optional ones set to '' or null or false in order to define the last important one.

Something like function test( $data_key, $number_of_results = 5, $classes = '' ){} makes sense because you can call test( 'my_key' ); and get arguably usable results.

Where if it was function test( $number_of_results = 5, $classes = '', $data_key ){}, you would make the semi-optional arguments before data_key required in a sense that they have to be passed to the function first: test( 5, '', 'my_key' );

Apart from the optional parameters that have to be at the end, the order of the parameters doesn't really matter.

Take this example.

function f($a, $b) { return $a - $b; }

and this is the same function with different order parameters.

function f($b, $a) { return $a - $b; }

You see. These two function returns the same value even if the order parameters are different.

In short, you just need to take care of mandatory and optional parameters. Mandatory parameters are always declared before the optional parameters. The order among the mandatory parameters don't matter at all. Same for optional parameters.

What matters most is consistency and meeting reasonable expectations. You really want people to avoid guessing which argument is which.

For example, bad design:

function add($list, $item)
function delete($item, $limit, $list)
function insert($count, $item, $list)
function push($item, $list)

Instead try and keep things consistent:

function add($list, $item)
function delete($list, $item, $limit)
function insert($list, $item, $count)
function push($list, $item)

You can also communicate expectations in the function name depending on how verbose that gets:

function deleteFrom($list, $item)

Or if you prefer a different order:

function insertItemInto($item, $list)

Where you're expecting that in actual use you'd see it called with variables having similar names so that the code reads better.

The order has no specific effect on performance. Only readability and understanding.