-2

I have variable containing both text and variables (as text) such as the following

$variable = 'something';

$string = 'this is my string that contains a $variable in text form, but i want the variable to actually contain $variable when i echo it';

and what im trying to achieve is by echoing that string, it turns the variables into the correct text which would make the above sentence something like

echo $string;

should result in

"this is my string that contains a something in text form, but i want the variable to actually contain something when i echo it"

Thank you for any help

Edit.

I've tried double quotes like echo "$string"; however its still echos it as text. im getting the variable via a simple query

$answer_id = $row0['id'];

<div class="text">
    <?php echo "$answer_id"; ?>
</div>

This still outputs a string without variables

Mystic
  • 147
  • 1
  • 14

1 Answers1

1

You should use double quotes

$string = "this is my string that contains a $variable in text form, but i want the variable to actually contain $variable when i echo it";

LIVE DEMO

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • 1
    Also, concatenation of using methods/parameters you can encapsulate the variable with `{}` braces. IE: `echo "this is my string that contains a {$obj->getVariable()} in text form..."` – Jaquarh May 06 '19 at 19:27
  • double quotes are the first thing i tried but its still echoing $variable as text – Mystic May 06 '19 at 19:58
  • @Mystic check here https://3v4l.org/PhEb1 – Rakesh Jakhar May 06 '19 at 20:00
  • thank you for the demo, that exactly what i expected however i cant work out what im doing wrong, i will have to check my code – Mystic May 06 '19 at 20:04