bernie2436
bernie2436

Reputation: 23901

What is the meaning of the nested of squared brackets indicating optional params in php documentation

I know that the square brackets indicate optional parameters in php documentation. Parameters in PHP documentation, squared brackets?

But does the nesting of brackets have any meaning? Are you supposed to resolve the inner-most bracketed parameters first the way you solve for stuff in the inner parenthesis in math?

In other words, are this

string http_build_url ([ mixed $url [, mixed $parts [, int $flags = HTTP_URL_REPLACE [, array &$new_url ]]]] )

and

string http_build_url ([ mixed $url] [, mixed $parts] [, int $flags = HTTP_URL_REPLACE] [, array &$new_url ] )

equivalent?

Upvotes: 0

Views: 226

Answers (3)

hek2mgl
hek2mgl

Reputation: 158040

No, because the , isn't required if only the first param is passed. That's why it is part of the next nested [, param ] declaration.

Example:

function abc($a);      --> [$a]         (just a is optional. no , required)
function abc($a, $b);  --> [$a [, $b]]  (the , is only necessary if you pass $b as well)

Params are nested, because the n+1th param needs the nth param to exist. Meaning you can't pass the second param if you didn't passed the first.

This notation is used across all programming languages and other technical documentation to describe optional arguments. I don't know where it is specified, would appreciate some input on that.

Upvotes: 2

Sammitch
Sammitch

Reputation: 32252

No, they are not equivalent.

PHP function parameters are strictly positional and function([$1] [,$2] [,3]) implies that you can simply call the function as function($3); which is incorrect.

If you wish to invoke a function with only the third optional argument then it must be called as function( , , $3);

This is why PHP.net's function references are listed as function([$1 [,$2 [,$3]]])

Upvotes: 1

Jessica
Jessica

Reputation: 7005

Parameters always go in the order they are listed. You don't "Resolve" parameters.

Read the answers in the question you linked to. It means each one is optional.

Upvotes: 1

Related Questions