TIL, PHP programming 

"Since PHP 5.3, it's possible to leave out the lefthand operand, allowing for even shorter expressions:

$result = $initial ?: 'default';

In this case, the value of $result will be the value of $initial, unless $initial evaluates to false, in which case the string 'default' is used."

Whaaaat. This has has been possible for 12 years, including my whole professional career as a PHP developer, and I didn't know that before today! :blobcat0_0:

@zatnosk that looks um less nice than ternary operator. Do you know the associativity of the operator?

@reynir it _is_ the ternary operator. Same left associativity.

It's just that one can apparently use the first operand as both the first and second operands, by emitting a second operand!

@zatnosk didn't php get associativity quite wrong with the ternary operator? Or did they fix that? :D

Like, a ? b : c ? d : e is read (a ? b : c) ? d : e IIRC instead of the more natural else-if: a ? b : (c ? d : e)

@reynir Yeah, it's the less useful associativity, and it haven't been "fixed", and I don't think it will be.

But we're getting other fancy operators such as ?? (with useful associativity!) and <=> and ?->

Sign in to participate in the conversation

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!