0

Using heredoc or nowdoc, how could I create $sting?

$x_value=2;
$string='$x='.$x_value.';';
exit($string);  //$x=2;

PS. I am aware of the slippery slope of script like this

user1032531
  • 24,767
  • 68
  • 217
  • 387
  • 1
    Without even needing to use heredoc/nowdoc `$string="\$x={$x_value};";` – Mark Baker Jul 26 '15 at 17:03
  • Thanks Mark, but I showed just a very small example, and wanted to use heredoc. – user1032531 Jul 26 '15 at 17:03
  • @Mark. Thanks. Escaped it just like you showed it, and it works perfect! – user1032531 Jul 26 '15 at 17:05
  • On this site, we discourage answers being edited into questions. Instead, you're encouraged to post an answer to your own question. That way, people can see at a glance that the question has an answer. – IMSoP Jul 26 '15 at 17:07
  • @IMSoP I will edit it. Just didn't want to take credit for an answer which Mark's comment was the inspiration. – user1032531 Jul 26 '15 at 17:10

1 Answers1

0

Per Mark's comment, the value which shouldn't be parsed should be escaped:

$x_value=2;
$string = <<<EOD
\$x=$x_value;
EOD;
exit($string);
user1032531
  • 24,767
  • 68
  • 217
  • 387