请问一个关于PHP很简单的问题,感激不尽
  源代码: fwrite($fp, "$_G[tid]\n");
  我想要的效果是:随即写入 $_G[tid]\n的次数,意思就是
  文本里面可能是$_G[tid]\n      也有可能是$_G[tid]\n$_G[tid]\n  或者$_G[tid]\n$_G[tid]\n$_G[tid]\n
我表达不是很好,举个例
$fp=test.txt
$g=测下一下!
能不能把$g用最节约系统资源的重复写入test.txt随即次数test.txt里面可能出现的是
测下一下!    或者测下一下!测下一下!         或者 测下一下!测下一下!测下一下! 
如果我要随即写入多次,最精简的代码该怎么写?
用for循环是不是非常消耗系统资源

解决方案 »

  1.   

    用for和array_rand
    哪个更加节约资源?
      

  2.   

    for ($i = 1; $i < 随机数; $i++) {
      $message .= "内容";
    }
    fwrite($fp, $message );
      

  3.   

    我觉得你用array_rand满足不了你的要求
      

  4.   

    $_G[tid] = 'string';//echo str_repeat("$_G[tid]\n", rand(1, 10));//如果写入就是$_G[tid],用单引号,重复1-10次,自己改
      

  5.   

    $rand = rand();
    $arr = array_fill(0,$rand,$_G[tid]\);
    $str = implode("",$arr);
      

  6.   

    $rand = rand();
    echo str_repeat("-=", $rand);
      

  7.   

    感谢各位的解答!!太感激了~!
    谢谢jianglin28290
    PIGer920
    amani11
    已经解决