0

I have a question about dynamic variables. (I have trouble searching because I have a hard time describing my problem)

In this example:

$x = 1;
$var = "A$x";
echo $var;    //prints 'A1'

Now my question is, is there a way to combine "computation" without adding another variable?

What I want to do is:

$x = 1;
$var = "A($x+1)";
echo $var;    //I want to output to be 'A2' but it gives 'A(1+1)'

I know that this works:

$var = "A".($x+1)

But this is not applicable to the program that I am doing. $var is initiated on the beginning of the program and will be used at the end waiting for any value of $x.

1 Answers1

-1

You need to concatenate your output.

$x = 1;
$var = "A". ($x + 1);
echo $var; 

In your example the "+1" is inside the quotes and thus is a literal string.

RemanBroder
  • 117
  • 1
  • 6