小编dox*_*guy的帖子

在php中回应会话变量

我知道在php中我可以在使用echo时将变量名放在带引号的字符串中,但我显然无法使用会话变量执行此操作.有谁能解释为什么?

这是代码,"冒犯"php注释掉了:

<?php
session_start();
$test = 100;
$_SESSION['test'] = 200;
?>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
  <p><?php echo($test."<br />");?></p>
  <p><?php echo("$test"."<br />");?></p>
  <p><?php echo($_SESSION['test']."<br />");?></p>
  <p><?php //echo("$_SESSION['test']"."<br />");?></p>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

100

100

200
Run Code Online (Sandbox Code Playgroud)

但是,如果我取消注释违规代码行:

  <p><?php echo("$_SESSION['test']"."<br />");?></p>
Run Code Online (Sandbox Code Playgroud)

我没有输出和以下错误:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in - on line 14
Run Code Online (Sandbox Code Playgroud)

因此,我可以继续我的快乐方式,知道如何正确地执行它(只是将会话变量保持在双引号之外),但我真的很想理解为什么这对会话变量不起作用.

谢谢!

php session-variables syntax-error

5
推荐指数
1
解决办法
3万
查看次数

使用Javascript的PHP - <script> </ script>标记内的PHP变量的范围是什么?

我正在尝试混合使用php和javascript,因为我的javascript需要在页面加载时访问一些php变量.我无法理解某些东西,而且我已经编写了一个代码示例.

<?php
session_start();
$test = 100;
$_SESSION['test'] = 200;
?>
<html>
<head>
<title>Test This Out</title>
</head>
<body>
<h1> Testing Javascript and PHP Mixed </h1>
<p>
<?php 
echo("The value of \$test is $test and the value of \$_SESSION['test'] is ");
?>
</p>
<p>
<script type="text/javascript">
<?php 
session_start();
if(isset($test)) echo("document.write('Non-session variable exists and is $test <br />');"); 
else echo("document.write('Non-session variable does not exist<br />');");
if(isset($_SESSION['test'])) echo("document.write('Session variable exists <br />');"); 
else echo("document.write('Session variable does not exist<br />');");
?>
</script>
</p>
</body>
</html> …
Run Code Online (Sandbox Code Playgroud)

javascript php scope

1
推荐指数
1
解决办法
2070
查看次数

标签 统计

php ×2

javascript ×1

scope ×1

session-variables ×1

syntax-error ×1