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

Call a PHP file from another one multiple times (+ file reading)

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

blankVote.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从数组中获取第一项。

我更喜欢使用 classes,所以我做了一个彩票 class,它允许您 "draw" 数组中的第一个项目。

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

现场观看: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 Lottery\Lotto;
$lotto = new Lotto([313328804459,
                159078851698,
                226414688415,
                380287830671,
                301815692106,
                2991355110,
        ]);
echo "Lotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Drawn: " . $lotto->draw()."\n";
echo "\nLotto status at this point\n";
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 here\n";
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Loading lotto from file\n";
//$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Drawn: ". $lotto->draw()."\n";
echo "\nLotto status at this point\n";
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 Lottery\Lotto;
/**
 * 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 file\n";
$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);

echo "Drawn: ". $lotto->draw()."\n";

var_dump($lotto);