function some_func(string|null $some_arg)
function some_func(?string $some_arg)
function some_func(string $some_arg = null)
function some_func(?string $some_arg = null)
CodePudding user response:
The string|null
union type actually converts to ?string
, so they are equivalent.
$string = null
(regardless of preceding type definition) sets a default value and makes the $string
argument optional. This allows you to use some_func()
without an argument, which will be interpreted as some_func(null)
.
So ultimately not much, and syntax here will depend on the usage of some_func
.
Edit:
As pointed out in a now-deleted comment, this one seems fishy:
function some_func(string $some_arg = null)
You'd think string
and the default value of null
don't play well, but this actually converts correctly to
function some_func(?string $some_arg = null)
So while odd, it is valid.
CodePudding user response:
The first definition, "function some_func(string|null $some_arg)", indicates that the function expects a parameter of type "string" or "null" for the variable "$some_arg". This is using the pipe symbol "|" to specify that the parameter can be either a string or null.
The second definition, "function some_func(?string $some_arg)", indicates that the function expects an optional parameter of type "string" or "null" for the variable "$some_arg". This is using the question mark symbol "?" before the type to specify that the parameter is optional.
The third definition, "function some_func(string $some_arg = null)", indicates that the function expects a parameter of type "string" for the variable "$some_arg", and that the parameter has a default value of "null".
The fourth definition, "function some_func(?string $some_arg = null)", indicates that the function expects an optional parameter of type "string" or "null" for the variable "$some_arg" and that the parameter has a default value of "null".