Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101620 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47975 invoked from network); 18 Jan 2018 22:52:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jan 2018 22:52:54 -0000 Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.44 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.44 mail-lf0-f44.google.com Received: from [209.85.215.44] ([209.85.215.44:42259] helo=mail-lf0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 25/29-12394-245216A5 for ; Thu, 18 Jan 2018 17:52:52 -0500 Received: by mail-lf0-f44.google.com with SMTP id q17so18502587lfa.9 for ; Thu, 18 Jan 2018 14:52:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=By3DFSqcmnNuVxhj89xViIzGDpDUtkUSzpTpLnyhRe4=; b=qic72cNDkhREEcggI2MGwH9t2b3/jzFAad2RIeVGxvFz5er7CKc5cDhIHL9S1rVfXq 3CS9+u3wWLvWcOn9+/EhWAsxLYri+4pq0It1ReSWW85gcTvk+f43dCjPPdEQs7e0fLzr QYwtjLoep8PeyABcIqB9T7vpIOdrFwc6zMLeowehDX2O80Lcsw8OFkvYBePwBxgyV8Gt UiamCz8e6JbTaQa5dBA70A93szfCiF4W8+ylqG7qU9OM0HCckTRQI/1kPpErE4bwcdgY bq9TbnjjKyz6k1SxClUewOunw4SxCMkjcnxyq53AX+SHJeJXVNwR/GhfFeltm2hobuHa 4JYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=By3DFSqcmnNuVxhj89xViIzGDpDUtkUSzpTpLnyhRe4=; b=ofOVvZv7NLXw25MoaBI1m13dyco09snmIYlk/dqZTfgmKv3hSrtl7IRjFdG+jWgYt3 O5WPSMq77j+EnTQPEenYGOXmo0wRXICwgr8fj08ywOnUhNyPvc8nATu2/fk64jfLGN26 2rvZ+c8p/eut+nSCAOam1i6HRzj0/RJcq9KiwAejB+MDV5ox/m710aoKTsVJP1eSZ23z 2wI9ra4iOvi1pcLA5uX7OULGo21xGRQtWbrq5GEzRMHni4QpTVvYfoEoGZgaObP7+95c AubKckcgxhSzN8z1Nz7PwoEdALn1LOTdtkwRyLHKBcTfJyTk887qrE6UxB3aMo43U1YG 6RSA== X-Gm-Message-State: AKwxytcGov/cJ85qk7h7ypwVLEwZ4xXfx0ZodkFwteo6jxOPjoxIRyGB NpHqGVBkDp2vO8bISMyJ5zTp1C+h X-Google-Smtp-Source: AH8x226pmIDSfj+JfWSEDk+lH/RlIz8kKgIvKYD3ZHmwlmd1T2F8r+mYrHYWOVOPbxwRRyWhGjGd8w== X-Received: by 10.25.26.200 with SMTP id a191mr49393lfa.35.1516315966838; Thu, 18 Jan 2018 14:52:46 -0800 (PST) Received: from mail-lf0-f49.google.com (mail-lf0-f49.google.com. [209.85.215.49]) by smtp.googlemail.com with ESMTPSA id l203sm1391603lfb.59.2018.01.18.14.52.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 14:52:45 -0800 (PST) Received: by mail-lf0-f49.google.com with SMTP id a204so20422969lfa.2 for ; Thu, 18 Jan 2018 14:52:45 -0800 (PST) X-Received: by 10.46.85.66 with SMTP id j63mr3338209ljb.55.1516315964686; Thu, 18 Jan 2018 14:52:44 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.221.20 with HTTP; Thu, 18 Jan 2018 14:52:24 -0800 (PST) Date: Thu, 18 Jan 2018 23:52:24 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: null coalesce addition assignment operator ??+= From: andreas@dqxtech.net (Andreas Hennings) The "Null Coalescing Assignment Operator" (or null coalesce assignment operator) was proposed and accepted in https://wiki.php.net/rfc/null_coalesce_equal_operator I propose even more such operators: null coalesce addition assignment ??+= (for strings and numbers) null coalesce subtraction assignment ??-= null coalesce increment ??++ null coalesce decrement ??-- null coalesce multiplication assingment ??*= ## Details Each block contains 3 equivalent statements. $x ??+= 5; // proposed new syntax ($x ??= 0) += 5; // syntax with null coalesce assignment operator $x = ($x ?? 0) + 5; // syntax with simple null coalesce operator $x ??+= 'suffix'; // proposed new syntax ($x ??= '') += 'suffix'; // syntax with null coalesce assignment operator $x = ($x ?? '') + 'suffix'; // syntax with simple null coalesce operator $x ??++; // proposed new syntax ($x ??= 0)++; $x = ($x ?? 0) + 1; $x ??*= 2; // proposed new syntax ($x ??= 1) *= 2; $x = ($x ?? 1) * 2; Note that in each case PHP needs to determine the "neutral element" of the operation. For string concat this is the empty string. For number addition this is 0. For number multiplication it is 1. ## Example: For me, the most common use case would be something like this: $fruitpacks = [ ['apples', 3], ['pears', 1], ['apples', 6], ['grapes', 22], ]; $totals_by_name = []; foreach ($fruitpacks as [$name, $amount]) { $totals_by_name[$name] ??+= $amount; // proposed new syntax } $totals_by_name_expected = [ 'apples' => 9, 'pears' => 1, 'grapes' => 22, ]; assert($totals_by_name === $totals_by_name_expected); ## Notes In PHP, the "+=" operator already behaves almost like "??+=", but adds a "Notice: Undefined offset" or "Notice: Undefined variable", if the left side is not defined yet. hhvm apparently does not produce this notice. https://3v4l.org/l0l0K