Sergei Beloglazov
Sergei Beloglazov

Reputation: 362

What does a question mark mean in PHP function parameter's default value?

There is a question mark in a parameter's default value for functions in PHP documentation. E.g.:

 assert(mixed $assertion, string $description = ?): bool

https://www.php.net/manual/en/function.assert.php

What does it mean?

Upvotes: 0

Views: 202

Answers (3)

Sergei Beloglazov
Sergei Beloglazov

Reputation: 362

According to the documentation https://www.php.net/manual/en/function.assert.php

assert() is a language construct in PHP 7

So it's not a function syntax. You can't use such a syntax in your own function.

Other answers are right about meaning of the question mark in the description of assert(). But the essence of the answer is "assert() is not a function", so my question is wrong by itself as it refers assert() as a function. Thanks for hints!

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53646

It means that the parameter is optional and has a dynamic default value. Usually, optional parameters have default values that are static, like this:

foo (string $bar = null): bool

Or this:

foo (string $bar = 0): bool

But in some cases, the default value changes depending on environment. These are shown by a question mark:

assert(mixed $assertion, string $description = ?): bool

And then the description of the parameter will tell you more details about what the exact value is:

From PHP 7, if no description is provided, a default description equal to the source code for the invocation of assert() is provided.

Upvotes: 2

Barmar
Barmar

Reputation: 782693

The $description argument is optional, but its default value is not a constant. The manual explains:

From PHP 7, if no description is provided, a default description equal to the source code for the invocation of assert() is provided.

This can't be easily expressed in the syntax summary, so they used ? as a placeholder.

Upvotes: 1

Related Questions