Is it legal to use double-quotes in string interpolation?
| From: | Gert Jan Schoneveld | Date: | Tue, 26 Aug 2025 13:05:18 +0000 |
| Subject: | Is it legal to use double-quotes in string interpolation? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-328750@lists.php.net to get a copy of this message | ||
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