rfc:isset-set-operator

PHP RFC: Isset/Set Operator

  • Version: 0.1
  • Date: 2013-11-24
  • Author: Chris London, me chrislondon co
  • Status: Draft (Inactive)

Introduction

Two new operators. ?= for setting the value of an unset or falsey variable. And ??: which acts like ?: except it it also checks is set.

Proposal

?= 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

Nothing yet.

Proposed PHP Version(s)

Next PHP 5.x

SAPIs Impacted

Not yet known.

Open Issues

Possible Alternatives To ?= Operator

  1. ?:=
  2. ||=
  3. @=

Possible Alternatives To ??: Operator

  1. ?::
  2. ?: (backward compatibility concerns)
  3. || (backward compatibility concerns)
  4. @:

Unaffected PHP Functionality

Future Scope

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.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/isset-set-operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1