Doc #79673 [Com]: "roughly equivalent to" without explaining the difference

[email protected] ("alisaudfxbpn53 at gmail dot com") Thu, 05 Jan 2023 08:11:37 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=79673&edit=1

 ID:                 79673
 Comment by:         alisaudfxbpn53 at gmail dot com
 Reported by:        kasperheij at hotmail dot com
 Summary:            "roughly equivalent to" without explaining the
                     difference
 Status:             Verified
 Type:               Documentation Problem
 Package:            Documentation problem
 PHP Version:        7.4.6
 Block user comment: N
 Private report:     N

 New Comment:

That was so amazing. 

(https://www.partycityfeedback.net/)github.com


Previous Comments:
------------------------------------------------------------------------
[2021-02-01 17:21:26] [email protected]

The problem is that the original RFC[1] was underspecified[2], and
the final implementation[3] turned out to be much harder than
expected.

Anyhow, I think the given code snippets are equivalent, even if
`$array instanceof ArrayAccess`.  However, if the literal 'key'
would be replaced with a function call, the function would only be
called once with the ??= operator, but may be called twice with
the "rough equivalent"[4].

> The "??=" operator isn't mentioned at all in the reference part
> of the documentation […]

This has been fixed in the meantime[5], but the given equivalence
is wrong.

[1] <https://wiki.php.net/rfc/null_coalesce_equal_operator>
[2] <https://github.com/php/php-src/pull/1795#issuecomment-387184002>
[3] <https://github.com/php/php-src/pull/3747>
[4] <https://3v4l.org/ITSK6>
[5] <https://www.php.net/manual/en/language.operators.assignment.php#language.operators.assignment.other>

------------------------------------------------------------------------
[2020-06-17 21:54:33] a at b dot c dot de

The "??=" operator isn't mentioned at all in the reference part of the documentation (under neither "comparison operators", where the base '??' is given; nor "assignment operators", which description of "combined operators" hasn't been updated). One of those would be the place to describe its behaviour and how it's different from the more long-winded form.

------------------------------------------------------------------------
[2020-06-10 18:18:27] alexinbeijing at gmail dot com

Interesting question!

I can't find any difference in the actual behavior, although the bytecodes executed by the interpreter are different in either case, so it is possible that there might be some (very small) difference in performance.

See:

https://3v4l.org/Z3dPV/vld#output
https://3v4l.org/2W7W1/vld#output

------------------------------------------------------------------------
[2020-06-04 11:21:51] kasperheij at hotmail dot com

Description:
------------
---
From manual page: https://php.net/migration74.new-features
---

```
<?php
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}
?>
```

"// is roughly equivalent to" says that there _is_ a difference without specifying this delta.





------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79673&edit=1