Two new operators. ?= for setting the value of an unset or falsey variable. And ??: which acts like ?: except it it also checks is set.
?= Will be a new operator that allows the user to set an unset or falsey variable. This handy operator will help avoid dreaded unset variable notices.
$foo ?= 'default'; // which is functionally equivalent to: $foo = (isset($foo) && $foo) ? $foo : 'default'; // or if (!isset($foo) || !$foo) $foo = 'default';
??: will be equivalent to the ternary short hand ?: except that it also checks for isset().
// $bar is unset $foo = $bar ?: 'other'; // Throws undefined notice $foo = $bar ??: 'other'; // Does NOT throw undefined notice // ??: is functionally equivalent to: $foo = (isset($bar) && $foo) ? $bar : $other;
This will be very helpful for echoing default variables in HTML like so:
<div class="<?= $user ??: 'guest' ?>"> ... </div>
Nothing yet.
Next PHP 5.x
Not yet known.
Possible Alternatives To ?= Operator
Possible Alternatives To ??: Operator
This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC.
Include these so readers know where you are heading and can discuss the proposed voting options.
Links to any external patches and tests go here.
If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.
Make it clear if the patch is intended to be the final patch, or is just a prototype.
After the project is implemented, this section should contain
Links to external references, discussions or RFCs
Keep this updated with features that were discussed on the mail lists.