<?php if(isset($_SESSION['logged_in'])) {
$somevar= "something";
?>
<script type="text/javascript">
var somevar2 = <?php echo $value; ?>;
</script>
<?php } ?>
I put the code inside <head>, the console shows somevar2 is undefined, I was expecting the script don't run because it put in a scope where it execute only when user is logged in. What is the cause of this problem?
somevar2, but it doesn’t exist because the shown<script>block has not been written into the HTML output for the not logged-in user.$valueis not declared anywhere in your code.