This is the code:
x = 8;
$y = "Value is $x <br>";
echo $y;
$x = $x + 2;
echo $y;
end the result is:
Value is 8
Value is 8
but I was expecting:
Value is 8
Value is 10
How to make $y behave in expected way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It behaves in the expected way. Your expectations are wrong.
This is an assignment statement. It assigns a string value. That string value is the result of a string expression. The expression gets evaluated, and then the strings is baked. It won’t change afterwards.
There is no standard way in PHP to have variable string values. One could create an object with
__toStringand%splaceholders for external variables. But that’s kind of a big workaround.