我正在试图弄清楚如何将rand()
函数的结果保存到10个不同的文件中。
这是我的代码:
<?php
for($i = 0; $i < 10; $i++)
{
$rand_valuec = rand(0,16);
echo "<b>Your Lucky Number is</b>: " . $rand_valuec . " <br />";
$fp = fopen("results.php", "w");
fwrite($fp, $rand_valuec);
fclose($fp);
}
示例输出:
Your Lucky Number is: 6
Your Lucky Number is: 16
Your Lucky Number is: 16
Your Lucky Number is: 8
Your Lucky Number is: 6
Your Lucky Number is: 10
Your Lucky Number is: 13
Your Lucky Number is: 5
Your Lucky Number is: 6
Your Lucky Number is: 5
results.php 包含:
5
我需要将每个结果保存到一个新文件中;例如1result.php
,2result.php
,3result.php
,4result.php
,...
,10result.php
。有任何想法吗? :-)谢谢!
答案 0 :(得分:4)
您可以将文件更改为:
$fp = fopen($i."result.php", "w");
答案 1 :(得分:2)
<?php
for($i=0; $i < 10; $i++)
{
$rand_valuec = rand(0,16);
echo "<b>Your Lucky Number is</b>: ".$rand_valuec." <br />";
$fp = fopen($i."results.php", "w");
fwrite($fp, $rand_valuec);
fclose($fp);
}
?>
答案 2 :(得分:0)
试一试:
<?php
$numberOfFiles = 10;
$numberOfLinesPerFile = 10;
$path = "/home/your-user/your-path/";
for ($fileIndex = 1; $fileIndex <= $numberOfFiles; $fileIndex++) {
$contentFile = "";
for ($i = 0; $i < $numberOfLinesPerFile; $i++) {
$rand_valuec = rand(0,16);
$content = "<b>Your Lucky Number is</b>: ".$rand_valuec." <br />";
}
file_put_contents($path . $fileIndex . "result.php", $content);
}