Warm tip: This article is reproduced from stackoverflow.com, please click
php

Can i use ternary operator on left of assignment operator?

发布于 2020-03-29 12:46:21

Like

(true ? $a : $b) = 5;

Or

(true ? &$a : &$b) = 5;

Or

&(true ? $a : $b) = 5;


("It looks like your post is mostly code; please add some more details.")

Questioner
gom
Viewed
34
Cid 2020-01-31 16:33

You can achieve quite the same result using, instead of the variable itself as return value of the ternary expression, the name of the variable.

(true ? $a : $b) won't return the variables, but their content.

This can be achieved like this :

<?php
${true ? "a": "b"} = 5;

echo $a; // outputs 5