Is it legal to use double-quotes in string interpolation?
Gert Jan Schoneveld <[email protected]> Tue, 26 Aug 2025 15:05:18 +0200
| Newsgroups | gmane.comp.php.general |
|---|---|
| Message-ID | <CADGd7o9a7ELbaOzL1-gsyUk+EM8xzjrBsfFLED-WTB95Xj4D4Q@mail.gmail.com> |
--00000000000099b3b5063d4453e4
Content-Type: text/plain; charset="UTF-8"
Dear all,
We have produced some php code that works fine when we run it but gives
problems when we scan it using the xgettext command line tool (0.23 or
higher). I reported this to them and the question now is: Is our code valid
php?
The code runs fine with PHP 8.4.11 (cli) from The PHP Group. Does this mean
it is valid php? Or is php similar to c++ with compiler dependent things
and/or undefined behavior when doing some other things?
I found two things in the specification on
https://phplang.org/spec/09-lexical-structure.html that may suggest that it
is not valid what we are doing:
1. This sentence: "The literal can contain any source character except
double-quote (") and backslash (\\), which can only be represented by their
corresponding escape sequence."
2. The definition only talks about "property-in-string". In my second
example we are not using a property but a method on an object.
I hope you can give me a definitive answer on whether this example is valid.
Example code:
<?php
class MyClass
{
public function method(string $p)
{
return $p;
}
}
$arr = [
"key" => 123
];
$obj = new MyClass();
echo "Test {$arr["key"]}" . PHP_EOL;
echo "Test {$obj->method("456")}" . PHP_EOL;
echo gettext("Hello") . PHP_EOL;
Regards,
Gert Jan Schoneveld
--00000000000099b3b5063d4453e4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Dear all,<br><br>We have produced some php code that =
works fine when we run it but gives problems when we scan it using the xget=
text command line tool (0.23 or higher). I reported this to them and the qu=
estion now is: Is our code valid php?<br><br>The code runs fine with PHP 8.=
4.11 (cli) from The PHP Group. Does this mean it is valid php? Or is php si=
milar to c++ with compiler dependent things and/or undefined behavior when =
doing some other things?<br><br>I found two things in the specification on =
<a href=3D"https://phplang.org/spec/09-lexical-structure.html">https://phpl=
ang.org/spec/09-lexical-structure.html</a> that may suggest that it is not =
valid what we are doing:<br><br>1. This sentence: "The literal can con=
tain any source character except double-quote (") and backslash (\\), =
which can only be represented by their corresponding escape sequence."=
<br><br>2. The definition only talks about "property-in-string". =
In my second example we are not using a property but a method on an object.=
<br><br>I hope you can give me a definitive answer on whether this example =
is valid.<br><br>Example code:<br><?php<br>class MyClass<br>{<br>=C2=A0 =
public function method(string $p)<br>=C2=A0 {<br>=C2=A0 =C2=A0 return $p;<b=
r>=C2=A0 }<br>}<br><br>$arr =3D [<br>=C2=A0 =C2=A0 "key" =3D> =
123<br>];<br><br>$obj =3D new MyClass();<br><br>echo "Test {$arr["=
;key"]}" . PHP_EOL;<br>echo "Test {$obj->method("456=
")}" . PHP_EOL;<br>echo gettext("Hello") . PHP_EOL;<br>=
</div><div><br></div><div><br></div><div><div dir=3D"ltr" class=3D"gmail_si=
gnature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div dir=3D"lt=
r"><div><span style=3D"font-family:arial,sans-serif;font-size:13px">Regards=
,<br></span></div><span style=3D"font-family:arial,sans-serif;font-size:13p=
x">Gert Jan Schoneveld</span></div></div></div></div></div>
--00000000000099b3b5063d4453e4--