简体   繁体   English

单引号或双引号的替代品?

[英]Alternatives for single or double quotes?

This example:这个例子:

$var = '<?php
    $_SESSION["a"] = 1;
    echo "<script>console.log($_SESSION["a"]);</script>";
?>'

returns an error because it thinks the quotes should stop before the a.返回一个错误,因为它认为引号应该在 a 之前停止。 If I used single quotes, the $var would instead stop in the middle.如果我使用单引号,$var 会停在中间。 Is there another alternative that could be used to wrap the 'a' in $_SESSION["a"] around without causing errors?是否有另一种替代方法可用于将 $_SESSION["a"] 中的 'a' 包裹起来而不会导致错误?

This is another simple way to fix your problem:这是解决问题的另一种简单方法:

<?php
session_start();

function setSession($name, $data)
{
    $_SESSION["$name"] = $data;
}

$var = setSession("a", 1);
?>

<script> 
console.log(<?php echo $var ?>);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM