user151841
user151841

Reputation: 18046

Is a default value for the first argument for a multi-argument function redundant?

I've seen code a few times like this

function my_func( $arg1 = '', $arg2, $arg3 ) { ...

There really is no way to use the default value for $arg, right? Since $arg2 and more are required, you're always going to have to specify a value for $arg1, correct? Which would mean that $arg1 would never use its default value?

Upvotes: 0

Views: 62

Answers (2)

Rajat Singhal
Rajat Singhal

Reputation: 11264

yes, you are right it is wrong to do so, even though php doesn't shows any error or warning in such case but any good ide will show warning...

enter image description here

Upvotes: 1

KingCrunch
KingCrunch

Reputation: 131981

PHP throws a strict warning and it is highly discouraged. However, it behaves like the first argument isn't optional. So at all you are right: The default value is never used, except you set it yourself (what doesn't feel like a default value anymore).

Upvotes: 4

Related Questions