如何将字符串放入会话中?
为。例如:$_SESSION_[$questioncounter+'question'] = $accepted;
如果为_$questioncounter = 2_,则表示$_SESSION_['2question']
发布于 2014-08-09 14:07:59
使用.(点)连接字符串和变量,并从$_SESSION_ try中删除_
$_SESSION[$questioncounter.'question'] = $accepted;所以完整的代码:
<?php
session_start();
$questioncounter = 2;
$accepted = 'yes';
$_SESSION[$questioncounter.'question'] = $accepted;
echo $_SESSION['2question'];  // yes
?>发布于 2014-08-09 15:10:06
嗯,有时需要串联,但在这种情况下不需要。在这种情况下,通过连接字符串来构建变量将是最糟糕的方法。
这就是你应该做的事情
// start session
if(!isset($_SESSION)){
    session_start();
}
// now, add your questions to the $_SESSION this way
$question = array('Is the sky really blue?', 'Should I stay or should I go?', 'Why I can\'t fly');
$_SESSION['question'] = $question;
// or this way
$_SESSION['question'][0] = 'Is the sky really blue?';
$_SESSION['question'][1] = 'Should I stay or should I go?';
...
// add their status this way
$_SESSION['question'][0]['accepted'] = 1; // or $_SESSION['question'][0]['status'] = 1;
$_SESSION['question'][1]['accepted'] = 0;
// and finally use them like this
echo $_SESSION['question'][0];
// or
if($_SESSION['question'][0]['accepted']){
    // say 'Bravo!'
}https://stackoverflow.com/questions/25215720
复制相似问题