多次从另一个 PHP 文件调用 PHP 文件(+文件读取)



我正在尝试做的是从blankVoteOB.txt中读取一个值,删除该值并多次重复此过程。

空白投票.php

global $cur_random;
for ($i=0; $i<2; $i++){
    include 'readWriteRandomBV.php';
    $random[$i]=$cur_random;
    echo $random[$i];
    echo ',';
}

readWriteRandomBV.php (从文件 blankVoteOB 读取当前行.txt然后删除该行(

<?php
    $dir = "blankVoteOB.txt";
    $file = fopen($dir, "r") or exit("Unable to open file!");
    global $lines;
    global $line_no;
    global $all_lines;
    global $writelines;
    global $cur_random;
    $lines = "";
    $line_no=0;
    while(!feof($file)) {
        $line_no++;
        $line = fgets($file);
        $all_lines .= $line."<br>";
        if($line_no > 1)
            $writelines .= $line;
        else{
            $curran = trim($line);
            $cur_random = $curran;
        }
    }
    fclose($file);
    $fh = fopen($dir, 'w') or die("ERROR! Cannot open $file file!");
    fwrite($fh, $writelines);
    fclose($fh);
?>

在运行 PHP 之前,blankVoteOB.txt 如下所示:

313328804459
159078851698
226414688415
380287830671
301815692106
2991355110

运行后,它变成:

159078851698
226414688415
380287830671
301815692106
2991355110
226414688415
380287830671
301815692106
2991355110

我想要的是:

226414688415
380287830671
301815692106
2991355110

我在这里做错了什么?

我建议你使用数组来存储选票,然后使用array_shift从数组中获取第一项。

我更喜欢使用类,所以我做了一个彩票类,它允许你"绘制"数组中的第一个项目。

如果您运行下面的代码并将文本输出与代码匹配,您可以看到它的作用。

在这里观看直播:https://ideone.com/T8stdB

<?php
namespace Lottery;
class Lotto {
    protected $lots;
    public function __construct($lots = []) 
    {
        $this->lots = $lots;    
    }
    public function draw() {
        return array_shift($this->lots);
    }
}
namespace BallotGuy;
use LotteryLotto;
$lotto = new Lotto([313328804459,
                159078851698,
                226414688415,
                380287830671,
                301815692106,
                2991355110,
        ]);
echo "Lotto status at this pointn";
echo "===========================================================n";
var_dump($lotto);
echo "===========================================================n";
echo "Drawn: " . $lotto->draw()."n";
echo "nLotto status at this pointn";
echo "===========================================================n";
var_dump($lotto);
echo "===========================================================n";
$saved = serialize($lotto);
//file_put_contents('ballots.txt',$saved);
/**
 * setting to null to emulate script ending 
 */
$lotto = null;
echo "Lotto set to null 'script' ends sort to speak heren";
echo "nLotto status at this pointn";
echo "===========================================================n";
var_dump($lotto);
echo "===========================================================n";
echo "Loading lotto from filen";
//$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);
echo "nLotto status at this pointn";
echo "===========================================================n";
var_dump($lotto);
echo "===========================================================n";
echo "Drawn: ". $lotto->draw()."n";
echo "nLotto status at this pointn";
echo "===========================================================n";
var_dump($lotto);
echo "===========================================================n";

没有多余的var_dumping的版本

https://ideone.com/YNKIM4 现场观看

<?php
namespace Lottery;
class Lotto {
    protected $lots;
    public function __construct($lots = []) 
    {
        $this->lots = $lots;    
    }
    public function draw() {
        return array_shift($this->lots);
    }
}
namespace BallotGuy;
use LotteryLotto;
/**
 * initialize lotto object
 */
$lotto = new Lotto([313328804459,
                159078851698,
                226414688415,
                380287830671,
                301815692106,
                2991355110,
        ]);
echo "Drawn: " . $lotto->draw()."n";
echo "Writing lotto to file. Ending script(j/k)n";
$saved = serialize($lotto);
file_put_contents('ballots.txt',$saved);
/**
 * setting to null to emulate script ending 
 */
$lotto = null;
$saved = null;
echo "Loading lotto from filen";
$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);
echo "Drawn: ". $lotto->draw()."n";
var_dump($lotto);

相关内容

  • 没有找到相关文章

最新更新