首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php会话中的字符串

php会话中的字符串
EN

Stack Overflow用户
提问于 2014-08-09 14:03:10
回答 2查看 74关注 0票数 0

如何将字符串放入会话中?

为。例如:$_SESSION_[$questioncounter+'question'] = $accepted;

如果为_$questioncounter = 2_,则表示$_SESSION_['2question']

EN

回答 2

Stack Overflow用户

发布于 2014-08-09 14:07:59

使用.(点)连接字符串和变量,并从$_SESSION_ try中删除_

代码语言:javascript
运行
复制
$_SESSION[$questioncounter.'question'] = $accepted;

所以完整的代码:

代码语言:javascript
运行
复制
<?php
session_start();
$questioncounter = 2;
$accepted = 'yes';
$_SESSION[$questioncounter.'question'] = $accepted;
echo $_SESSION['2question'];  // yes
?>
票数 4
EN

Stack Overflow用户

发布于 2014-08-09 15:10:06

嗯,有时需要串联,但在这种情况下不需要。在这种情况下,通过连接字符串来构建变量将是最糟糕的方法。

这就是你应该做的事情

代码语言:javascript
运行
复制
// 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!'
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25215720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档