每次在php中的其他字符串回声?

时间:2016-08-12 03:05:36

标签: php

假设我有一个字符串数组,如何在刷新时逐个回显

 session_start();

if(isset($_SESSION["names"])){
    $_SESSION["names"] = $_SESSION["names"] + 1;
}
else{
    $_SESSION["names"] = 1;
}

    $names = array("Adrian","Bill","Mac","John");

   echo $_SESSION["names"]; 

我的代码很乱,例如。我需要每次都回显新名称而不是随机

2 个答案:

答案 0 :(得分:1)

这对我有用:

<?php
   session_start();

   if(isset($_SESSION["names"])){
       $_SESSION["names"] = $_SESSION["names"] + 1;
   }
   else{
       $_SESSION["names"] = 0;
   }

   $names = array("Adrian","Bill","Mac","John");

   echo $names[$_SESSION["names"] % count($names)];
?>

答案 1 :(得分:0)

怎么样

session_start();

if(!isset($_SESSION["echoedNames"])) {
$_SESSION["echoedNames"] = array();
}

while (!in_array($randomName = $names[rand(0, count($names))], $echoedNames) {
echo $randomName;
$_SESSION["echoedNames"][] = $randomName;
}

或类似的东西..基本上是两个数组,一个已显示名称并检查是否在回显之前显示...