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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,