【问题标题】:Parsing only some variables with heredoc or nowdoc使用heredoc或nowdoc仅解析一些变量
【发布时间】:2015-10-16 20:13:29
【问题描述】:

使用 heredoc 或 nowdoc,我如何创建 $sting?

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

PS。我知道这样的脚本很滑

【问题讨论】:

  • 甚至不需要使用heredoc/nowdoc $string="\$x={$x_value};";
  • 谢谢马克,但我只展示了一个非常小的例子,并想使用heredoc。
  • @马克。谢谢。就像您展示的那样逃脱了它,并且效果很好!
  • 在这个网站上,我们不鼓励将答案编辑成问题。相反,我们鼓励您发布自己问题的答案。这样,人们一眼就能看出问题有答案。
  • @IMSoP 我会编辑它。只是不想把马克的评论作为灵感的答案归功于自己。

标签: php string heredoc nowdoc


【解决方案1】:

根据马克的评论,不应该被解析的值应该被转义:

$x_value=2;
$string = <<<EOD
\$x=$x_value;
EOD;
exit($string);

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 2014-08-22
    • 2012-06-24
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多