doudun1029 2014-05-18 20:57
浏览 39
已采纳

如何在PHP中回显随机文本? [关闭]

I have about 300 lines of text that need to be echoed randomly.

Here's my current code:

<?php 

$lines = array(
'Line 1',
'Line 2',
'Line 3'
);

$powered = echo $lines[rand(0, count($lines)-1)];

echo $powered;

?>

I know that the issue is probably with line 9 because I'm not sure how to assign the echoing to a variable. I need to be able to include this file in several others and echo $powered to get a random line.

Any help would be really appreciated.

  • 写回答

1条回答 默认 最新

  • duan1983 2014-05-18 21:07
    关注
    $lines = array("one", "two", "three","four");
    for($i =0; i < count($lines)-1;i++){
    
    $line = $lines[rand(0, count($lines)-1)];
    $lines = array_diff($lines, array($line));//Use this to remove the index of array
    }
    

    i wrote this in the stack overflow chat so there might be a problem or two. though, all looks well to me.

    Was this what you were looking for?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题