I wonder if we can do some arithmetic operation, like $x+$y, within a string quote?
// 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?
// 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.
You will need to do it like this, change the below line from
echo "$x + $y = {$x}+{$y}"; // 5 + 11 = 5+11
To
echo "$x + $y = ".($x+$y); // 5 + 11 = 16
use the following format and must like
$a=10;$b=12;
echo "$a+$b"."=".($a+$b);
You have posted that Your required output is
// 5 + 11 = 16
.
So You may try the below code.
$x = 5;
$y = 11;
echo "$x + $y=".($x+$y);
I think this will give Your required output.