5

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 =
weeix
  • 1,359
  • 1
  • 14
  • 17
  • 6
    Why does it need to be inside the quotes? What are you trying to achieve? Why not concatenate the answer? `echo "$x + $y = ".($x + $y);` – naththedeveloper Aug 12 '13 at 07:24
  • 4
    http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php - but whatever you do, don't let anyone convince you to use eval() – Mark Baker Aug 12 '13 at 07:25
  • 1
    @FDL I'm trying to put it in a ternary operator. – weeix Aug 12 '13 at 07:27
  • Can you show that code? You can still use concatenation within ternary – naththedeveloper Aug 12 '13 at 07:30
  • To add to Mark Baker's comment, it just so happens there's a question (well, probably a couple) about the [evils of eval](http://stackoverflow.com/q/951373/) on this very site. – outis Aug 12 '13 at 07:35
  • Sorry, my mistake; I forget to put `$x+$y` within a parenthesis, so echo only outputs `16`. Thank you for reminding me. – weeix Aug 12 '13 at 07:46

6 Answers6

8

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Wow, that's cool! There actually is a way to do this, although it looks quite complicated to me. I'm curious to know how `${}` and `0*${0}` work sir. – weeix Aug 12 '13 at 10:03
  • 2
    The square brackets allow you to provide a string expression as variable name. Here the expression is `0`, so the variable is `$0` (which can not be written without the brackets otherwise it would be an invalid variable name syntax). As you can put an expression in the brackets, it is also used to assign to that `${0}` variable the result of `$x + y$` which then multiplied by zero gives zero (does not work with INF or NAN calculations). – hakre Aug 12 '13 at 10:32
0

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
Rohit Chemburkar
  • 1,019
  • 8
  • 13
0

try this below code :

<?php
    $x =5;
    $y =11;
echo "$x + $y = ".($x+$y);
?>

and also refer this Fiddle

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
0
echo "$x + $y = ".($x+$y);

You may do it like this.

sanfen
  • 1
0

use the following format and must like

                $a=10;$b=12;
                 echo "$a+$b"."=".($a+$b);
alok.kumar
  • 380
  • 3
  • 11
  • This format works as well: https://eval.in/private/61b368ca6a5e88 - so yours can't be a must. – hakre Aug 12 '13 at 07:47
-1

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.

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
  • 2
    It is not even syntactically right. $x and $y won't evaluate in single quotes, equal sign will give parse error, it needs to be quotes too, the result should be concatenated. – Royal Bg Aug 12 '13 at 07:31
  • @RoyalBg Yes You were 100% correct. When I also tried it myself the previous code also giving error. But Now I have edited my post. And now the above code giving the expected result from my side. – Ripa Saha Aug 12 '13 at 07:41