PHP RFC: Isset/Set Operator
- Version: 0.1
- Date: 2013-11-24
- Author: Chris London, me chrislondon co
- Status: Draft (Inactive)
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>
Backward Incompatible Changes
Proposed PHP Version(s)
Next PHP 5.x
Not yet known.
Possible Alternatives To ?= Operator
Possible Alternatives To ??: Operator
- ?: (backward compatibility concerns)
- || (backward compatibility concerns)
Unaffected PHP Functionality
This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC.
Proposed Voting Choices
Include these so readers know where you are heading and can discuss the proposed voting options.
Patches and Tests
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
- the version(s) it was merged to
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
Links to external references, discussions or RFCs
Keep this updated with features that were discussed on the mail lists.