简体   繁体   中英

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. If I used single quotes, the $var would instead stop in the middle. Is there another alternative that could be used to wrap the 'a' in $_SESSION["a"] around without causing errors?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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