Alternative values are a powerful way of safely setting a value without relying on the
built-in is_set
function. Alternative values were added in ZPE version
1.7.11 (September 2019).
An alternative value is a syntax that allows the programmer to define a fall back
value in the case that a value is undefined
or
null
.
The alternative value is specified with the ??
syntax after an expression:
⟨expression1⟩ ?? ⟨expression2⟩
For example:
YASS
$search = input("Please insert the username you are looking for.") $users = ["jamie1" => "Jamie Balfour", "john1" => "John Balfour"] print($users[$search] ?? "User not found")
If the user were to input a username that does not exist in the array, the print method would not print
undefined
but would instead print User not found
because of the ??
specifying an alternative value.
Comments