Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

muqriz1010's avatar

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.

0 likes
3 replies
jlrdw's avatar

Look at php wiki and changes.

danichurras's avatar

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.