dougupang0901 2018-01-28 10:19
浏览 82
已采纳

如何连续将用户输入数据推送到$ _SESSION数组然后检索它?

I am trying to get my head around the way PHP sessions work. I am simply trying a hangman game where the first player inputs a secret word, a second player then starts to guess one letter at a time.

Let's says that the secret word is cat, player two tries, c then a then s. I would like the final output to be c a _.

  <?php
session_start();

global $word;
global $guess;
global $hangman;


if (isset($_POST['player1'], $_POST['word'])) {
    $_SESSION['word'] = $_POST['word'];
    $word = $_SESSION['word'];
}

if (isset($_POST['player2'], $_POST['guess'])) {
    $_SESSION['guess'] = $_POST['guess'];
    $guess = $_SESSION['guess'];
}

$counter = 0;
$word = strtolower($_SESSION['word']);
$guess = strtolower($_SESSION['guess']);
echo $word . "<br>";
$found = [];

$counter = 0;

for ($i = 0; $i < strlen($word); $i++) {
    if ($counter < strlen($word)) {
        if (strpos($word[$i], $guess) !== false) {
            $found[] = $guess;
            $counter++;
        } else {
            $found[] = " _ ";
        }
    }
}

  print_r($found);

Instead of printing out all the contents the found array, I am only getting one single letter to print every time. However, I would like to see the full concatenated string as I've mentioned above.


Here is what the output looks like:

enter image description here

  • 写回答

6条回答 默认 最新

  • dongmi1872 2018-02-03 17:12
    关注

    How to continuously push user input data into $_SESSION array and then retrieve it?

    An easy way to do that is by binding a variable with an element in the $_SESSION array. This is a useful trick that you won't find in the manual. A simple example:

    $foo =& $_SESSION['foo'];
    

    That assignment will bind $foo and $_SESSION['foo'] to the same value, so every update to $foo is also an update to $_SESSION['foo'].

    Here is an example usage in the style of your hangman game:

    <?php
    session_start();
    
    $word =& $_SESSION['word'];   //bind $word with $_SESSION['word']
    $found =& $_SESSION['found']; //bind $found with $_SESSION['found']
    
    if (isset($_REQUEST['word'])) {
        $word = str_split($_REQUEST['word']);
        $found = array_fill(0, count($word), '_');
    }
    if (isset($_REQUEST['guess'], $word, $found)) {
        $guess = array_fill(0, count($word), $_REQUEST['guess']);
        $found = array_replace($found, array_intersect($word, $guess));
    }
    
    echo join(' ', $found);
    

    With the binding, the values of $word and $found will be saved as a part of the session data, without the need to do $_SESSION['word'] = $word; and $_SESSION['found'] = $found; anywhere in the script.

    Note that I use $_REQUEST instead of $_POST to make it easier to test with a browser. Modify as desired.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题