John Book John Book - 3 months ago 6
PHP Question

Echo each time other string in php?

Let's say I have an array of strings how do I echo one by one at refresh

session_start();

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

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

echo $_SESSION["names"];


My code is messy just for example .I need to echo each time new name but not random

Answer

This works for me:

<?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)];
?>
Comments