Doc #71927 [Com]: string interpolation with ${}
[email protected] ("autoupdatedaily at gmail dot com") Tue, 16 May 2023 10:20:26 +0000
| Newsgroups | php.doc.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=71927&edit=1
ID: 71927
Comment by: autoupdatedaily at gmail dot com
Reported by: 1783946138 at qq dot com
Summary: string interpolation with ${}
Status: Open
Type: Documentation Problem
Package: Scripting Engine problem
PHP Version: 7.0.5
Block user comment: N
Private report: N
New Comment:
Auto Advice Update are sharing latest news about auto, maintaince, car, bus, bilkes, electric bikes and scooters etc. More info to visit: (https://autoadviceupdate.com)github.com
Previous Comments:
------------------------------------------------------------------------
[2016-03-31 06:39:36] [email protected]
Huh. https://3v4l.org/jQXFc
I think this is intentional. Or at least a side-effect of intentional design.
${expression} basically has two behaviors:
1. If "expression" is a valid variable name then the value of $expression (or empty string if not defined) is interpolated into the string.
2. If "expression" is not a valid variable name then expression is evaluated and the result is expected to be the name of a variable which can be interpolated.
The first three variations are not valid variable names so the expressions "great ", " great", and " great " are evaluated using the constant and the result is effectively ${my} = "my value".
The fourth variation is a valid variable name and so is interpolated as $great = "fantastic".
This is a little different from how the same syntax works outside of strings: the "expression" is *always* treated as an expression. https://3v4l.org/IjhcH
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
I feel like that section could go into more detail about how the {$} and ${} syntaxes work and about what all is and is not allowed with each.
------------------------------------------------------------------------
[2016-03-31 01:27:52] 1783946138 at qq dot com
Description:
------------
The unstable result to use "{}" in string interpolation.It's just because useing space.
Test script:
---------------
error_reporting(E_ALL);
define('great','my');
$great='fantastic';
$my='my value';
echo "This is ${great }<br/>";//output:my value
echo "This is ${ great}<br/>";//output:my value
echo "This is ${ great }<br/>";//output:my value
echo "This is ${great}<br/>";//output:fantastic
Expected result:
----------------
none
Actual result:
--------------
none
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71927&edit=1