doubijiao2094 2015-01-08 07:43
浏览 86

为什么在eval语句中变量之前存在反斜杠?

While learning PHP online I am stopped at this eval function please help me. Why is there is a slash before $str2 in eval statement?

<?php
$string = "beautiful";
$time = "winter";

$str = 'This is a $string $time morning!';
echo $str. "<br>";

eval("\$str2 = \"$str\";");
echo $str2;
?>
  • 写回答

4条回答 默认 最新

  • dongraa1986 2015-01-08 07:49
    关注

    The slash escapes the dollar sign, else in double quotes the dollar signs starts a variable name.

    echo $var; // print the content of $var
    echo "$var"; // print the content of $var
    echo "\$var"; // print '$var'
    echo '$var'; // print '$var'
    

    Other thing is that you should find another book/tutorial for studying. Usign eval is unrecommended and in this case bad.

    The last two lines of your code should be:

    $str2 = $str;
    echo $str2;
    

    OR just

    echo $str;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了