Is this a proper syntax (PHP 7.3)
I got the following example from chatgpt:
Example 1
$hasValue = true;
$hasValue ?: 'yes it does' (This syntax works)
Normally I would use ternary operator as such:
Example 2
$isAdult = ($age >= 18) ? "Adult" : "Minor";
I could not find examples online for Example 1 although it's working.
Look at php wiki and changes.
honestly this is a monstrosity 😅. As for code readability you'd expect $hasSomething and $isSomething to return booleans.
As regards of the ternary, it does not work like that. Suppose:
$hasValue = true;
$something = $hasValue ?: 'yes';
// prints out true
$hasValue = false;
$something = $hasValue?:'yes';
//prints out 'yes'
Stick to the "Example 2" but beware of variable naming, it really makes difference if you follow a convention.
Please or to participate in this conversation.