如何使一个php文件不止一次工作

时间:2018-08-24 03:15:48

标签: php

我有一个生成器,它生成一个随机的电话号码,这是我的代码。

<?php
    limit_phone = 8; //limit phone number
    $randomphone = substr(str_shuffle(str_repeat("0123456789", $limit_phone)), 0, $limit_phone); //random numbers
    $randomphonecomp = array('010','011','012','015'); // company
    $randomphonecomp1 = array_rand($randomphonecomp);
    $phonefinaal = $randomphonecomp[$randomphonecomp1] . $randomphone;
    echo $phonefinaal. "<br>";
?>

我想要的是,当我运行此文件多次以上时,意味着我希望该文件不停机地自动生成,直到我从浏览器停止页面为止

2 个答案:

答案 0 :(得分:0)

您可以使用循环来生成指定数量的电话号码,不要使用无限循环,它会杀死您的浏览器,并且您将无法停止循环。

<?php
$total_numbers = 100;
$i=1;
while($i<=$total_numbers){
    $limit_phone = 8; //limit phone number
    $randomphone = substr(str_shuffle(str_repeat("0123456789", $limit_phone)), 0, $limit_phone); //random numbers
    $randomphonecomp = array('010','011','012','015'); // company
    $randomphonecomp1 = array_rand($randomphonecomp);
    $phonefinaal = $randomphonecomp[$randomphonecomp1] . $randomphone;
    echo $phonefinaal. "<br>";
$i++;
}
?>

答案 1 :(得分:0)

正如@bcperth所说,您很可能需要使用页面中的Javascript生成电话号码,可能是这样的:

var divNumbers = document.getElementById("numbers")
var btnStop = document.getElementById("stop")

function generate() {
  var randomphone = ""
  for(var i = 0; i < 8; ++i) randomphone += Math.floor(Math.random()*10)
  var randomcomp = ['010', '011', '012', '015'][Math.floor(Math.random()*4)] + ""
  divNumbers.innerHTML += randomcomp + randomphone + "<br>"
}

generate() // first generated number
var intervalGen = setInterval(generate, 2000) // others generated number

function stop() {
  clearInterval(intervalGen)
}

btnStop.addEventListener("click", stop)
#stop {
  border: 1px solid grey;
  padding: 2px 5px;
  cursor: pointer;
}
<span id="stop"> STOP </span>
<div id="numbers"></div>

文档: