php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72047 Support a "conditional assignment" operator like ruby's `||=`
Submitted: 2016-04-18 08:38 UTC Modified: 2016-04-18 10:05 UTC
From: oded at geek dot co dot il Assigned: requinix (profile)
Status: Closed Package: Variables related
PHP Version: 7.0.5 OS: Linux
Private report: No CVE-ID: None
 [2016-04-18 08:38 UTC] oded at geek dot co dot il
Description:
------------
---
From manual page: http://www.php.net/language.operators.assignment
---

I often find myself setting default values to variables using syntax of the form:

$a = $a ?? "default";

The same code in Ruby would be written as

a ||= "default"

Which is more concise and reduces boilerplate. It would be great if PHP could support such shorthand syntax. For example, how about a "null coalescing" assignment operator:

$a ??= 'default'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-18 09:27 UTC] requinix@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: requinix
 [2016-04-18 09:27 UTC] requinix@php.net
Already been accepted, though the PR for it is still pending. https://wiki.php.net/rfc/null_coalesce_equal_operator
 [2016-04-18 10:05 UTC] oded at geek dot co dot il
I'm happy to see that I'm not breaking any new ground here :-) 

waiting with anticipation.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 10:01:24 2019 UTC