-1

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?

mdja123cs
  • 21
  • 3

1 Answers1

0

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>
amlxv
  • 1,610
  • 1
  • 11
  • 18