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

时间:2018-03-12 18:59:32

标签: php file-manipulation

我要做的是从 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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

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

我更喜欢上课,所以我做了一个彩票课,让你可以画画&#34;数组中的第一项。

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

在此处观看: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";

没有superfluos 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);