About some missing assignement operators in php 8

Created on 20 February 2024, 10 months ago
Updated 7 March 2024, 10 months ago

Problem/Motivation

0. In πŸ“Œ Use null coalescing assignment operator Active two cases are considered,
A0:
$A = $A ?? $B;
B0:

if (!isset($A)) {
  $A = $B;
}

and these two cases are changed to,
$A ??= $B;

1. Now what to do if instead we meet,
A1:
$A = $A ?: $B;
B1:

if (!isset($A)) {
  $A = $A ?: $B;
}

C1:

if (empty($A)) {
  $A = $B;
}

? A difference between ?? and ?: is that ?? looks for the existence, whereas ?: does not. That is . ?? ~ isset(.) and . ?: ~ if (.), and not !empty(.).

Then to transform the code like for ?? we expect some thing like,
$A := $B;
for A1, and for B1 and C1,
$A ?:= $B;
with two different assignment operators: . := ~ if (.) and . ?:= ~ !empty(.) ~ isset(.) && if (.).

However no such assignment operators exist in PHP 8.2.

2. The !isset could be replaced by is_null or === NULL: the existence part is not considered,
A2:
$A = !is_null($A) ? $A : $B;
B2:

if (is_null($A)) {
  $A = $B;
}

Or with $A === NULL instead of is_null($A).
Suppose a corresponding assignment operator ?=, the above codes A2 and B2 would be rewritten,
$A ?= $B;
However no such assignment operator exists in PHP 8.2.

Finally all these cases could be summarized in an array,

----------------------------
       Ask if exists ?     |
----------------------------
    Yes      |     No      |     
--------------------------------------
   ??=  (1)  |   ?=  (2)   |  NULL ? | 
--------------------------------------
   ?:=  (2)  |   :=  (2)   |  TRUE ? |
--------------------------------------
 Assignment operators:
(1) : exists in php 8.2
(2) : does not exist/missing in php 8.2

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated about 2 hours ago

Created by

πŸ‡«πŸ‡·France Chris64 France

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Chris64
  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Hi Chris64,
    Just for our info, when you created this issue, were you presented with this standard template? I thought that coding_standards issues had a new custom template which is designed to assist the process of changing standards. But you seem to have managed to use the old ordinary template.

  • πŸ‡«πŸ‡·France Chris64 France

    @jonathan1055, may be the problem is project and component are not well chosen... Changed for Drupal core and base system.

  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    What is this issue actually aiming to achieve? If it’s a coding standard you want for the $A = $A ?: $B case then β€œCoding Standards” is of course the correct project. Otherwise, what is the goal of this issue? Could you please clarify in the issue description?

    If you actually want to create a coding standard for $A = $A ?: $B then I vote against it. In my opinion, there is no clearly preferrable version here and we definitely cannot and shouldn’t try to provide a standard for every possible piece of code.

  • Status changed to Closed: duplicate 10 months ago
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Hard to determine what this issue is asking for.

    We have πŸ“Œ Require short ternary (Elvis operator) syntax Postponed for making use of more ?:

    About the issue title, "About an ?: assignment operator.", ?: is not for assignment.

    ?: is specifically for testing FALSE. Whereas ?? or ??= is for non-existent or NULL.

    Closing based on πŸ“Œ Require short ternary (Elvis operator) syntax Postponed

  • Status changed to Active 10 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    Sorry, my purpose is not clear enough. Even is obscure. May be it should be ignored since it is about about some thing that does not exist, but I don't think it should be closed as duplicate. It is not Elvis operator. Yes, ?: is not an assignment operator, but I would like to speak about an assignment operator for ?:.

    Here a clearer exposition,

    -------------------------------------------------
    symbol | ternary operator | assignment operator |
    -------------------------------------------------
      ??   |        ??        |        ??=          |
    -------------------------------------------------
      ?!   |        ?!        |        ?!=  (1)     |
    -------------------------------------------------
    (1) : does not exist/missing in php 8.2

    The purpose of this issue is about case (1).

  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡«πŸ‡·France Chris64 France

    New IS with some changes and less mistakes.

  • Status changed to Postponed: needs info 10 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Like #7 I am not sure what is to be achieved. Can you complete the proposed change section? Or provide before and after examples?

Production build 0.71.5 2024