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条)

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。