在php中使用文件和队列

时间:2017-06-28 11:02:00

标签: php file

在这段代码中,我使用文件来存储数字,方法是将它们分开,用逗号分隔并执行数组操作。但是我只需要在文件中存储前6个数字,从第7次开始输入的数字应该最后添加到文件并删除文件中的第一个数字,就像push和pop一样。我怎么能这样做?

<?php 
$number=$_POST['number'];
if(isset($_POST['number'])) 
{
  $data = $_POST['number'] . ",";
  file_put_contents('data_file.txt', $data, FILE_APPEND | LOCK_EX);
}
$text = file_get_contents('data_file.txt');
$a1 = explode(',',$text);
$k=count($a1);
$i=$k-1;
if($i== 1)
{
  echo "Hi User your Lucky number Wins,CONGRATS!!!". "<br><br>";
  echo "Your Number :" .$a1[0]. "<br><br>";  
}
elseif(($i>1) && ($i<=5))
{
  if($a1[$i-1]==$a1[$i-2])
   {
     echo "Hi User your Lucky number Wins,CONGRATS!!!" ."<br><br>";
     echo "Your Number :" .$a1[$i-1]. "<br><br>"; 
   }
  else 
  {
    echo "Hey User you lose!!!SORRY!!!" . "<br><br>";
    echo "Your Number :" .$a1[$i-1]. "<br><br>"; 
  }
}
elseif($i>=6)
{
  $sum=0;
  for($j=$i-1;$j>$i-6;$j--)
   {
     $sum=$sum + $a1[$j-1];
   }
   echo "Sum is : $sum";
   echo "<br><br>";
   $average = $sum/5;
   echo "Average is : $average";
   echo "<br><br>";
   $result2=round($average);
   if($result2==$number)
    {
      echo "Hi User your Lucky number Wins,CONGRATS!!!" ."<br><br>";
      echo "Your Number :" .$number. "<br><br>"; 

    }
   else 
    {
      echo "Hey User you lose!!!SORRY!!!".  "<br><br>";
      echo "Your Number :" .$number. "<br><br>"; 
     }
 }
else
{
  echo "error";
}
?>

0 个答案:

没有答案