文字列の引用符内で $x+$y のような算術演算を実行できないでしょうか?
// Expected result is:
// 5 + 11 = 16
echo "$x + $y = {$x+$y}"; // Parse error
echo "$x + $y = {$x}+{$y}"; // 5 + 11 = 5+11
echo "$x + $y = ${x+y}"; // 5 + 11 =
文字列の引用符内で $x+$y のような算術演算を実行できないでしょうか?
// Expected result is:
// 5 + 11 = 16
echo "$x + $y = {$x+$y}"; // Parse error
echo "$x + $y = {$x}+{$y}"; // 5 + 11 = 5+11
echo "$x + $y = ${x+y}"; // 5 + 11 =
I wonder if we can do some arithmetic operation, like $x+$y, within a string quote?
Yes you can. You can just let PHP caclulate the arithmetric operation and then assign it to a variable and output it.
You can also do that inside a double-quoted string (Demo):
<?php
// @link http://stackoverflow.com/a/18182233/367456
//
// Expected result is:
// 5 + 11 = 16
$x = 5;
$y = 11;
echo "$x + $y = ${0*${0}=$x + $y}"; # prints "5 + 11 = 16"
However that is probably not what you're looking for.
このようにする必要があります。以下の行を
echo "$x + $y = {$x}+{$y}"; // 5 + 11 = 5+11
に
echo "$x + $y = ".($x+$y); // 5 + 11 = 16
echo "$x + $y = ".($x+$y);
あなたはこのようにするかもしれません。
次の形式を使用し、好きでなければなりません
$a=10;$b=12;
echo "$a+$b"."=".($a+$b);