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

Time:05-03

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related