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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: oded at geek dot co dot il
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 14:01:37 2025 UTC