Doc #81511 [Opn->Nab]: Evaluation order for . operator should be specified
Edit report at https://bugs.php.net/bug.php?id=81511&edit=1
ID: 81511
Updated by: requinix@php.net
Reported by: dean at omnivisiontechnology dot com
Summary: Evaluation order for . operator should be specified
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Unknown/Other Function
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
It isn't specified because it isn't guaranteed. It's generally left to right, sure,
but the exact behavior may change in the future if PHP alters how it parses and executes code. Not
guaranteeing behavior also potentially allows caching or optimization extensions more freedom in the
benefits they can offer.
It's a bad idiom. Don't use it. "Clever" code doesn't benefit anyone.
Previous Comments:
------------------------------------------------------------------------
[2021-10-07 06:56:50] dean at omnivisiontechnology dot com
Description:
------------
---
From manual page: https://php.net/language.operators.string
---
In general, PHP doesn't specify the order of evaluation for operators.
However, order of evaluation for the string concatenation operator specifically, does always seem
to be left then right.
If this can indeed be guaranteed, then it should be specified in the documentation, because it
enables the very common coding idiom:
$string = 'something'.($complicatedSubstring = 'something else').'more
stuff'.$complicatedSubstring;
Test script:
---------------
$s = '[Very bad!]'
$x = 'bar '.($s = 'foo').' bar bar '.$s.' bar';
Expected result:
----------------
$x should be 'bar foo bar bar foo bar'.
It'd be very unexpected for it to be 'bar foo bar bar [Very bad!] bar',
But that's what could happen if the . operator evaluated the RHS first... which fortunately it
doesn't appear to.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81511&edit=1
Thread (6 messages)