Erik Eldagsen Marquez Erik Eldagsen Marquez - 21 days ago 7
PHP Question

How to echo same sentence random times in php?

I need to implement this method to my webpage. I want to echo the same sentence random times every time someone refresh my php webpage.

For example: Hello World!

Answer

You can do:

<?php
$min = 1; // Minimum 1 time
$max = 10; // Maximum 10 times
$string = "Hello world!\n"; // Print Hello World! and a new line
$x = rand($min,$max); // Make the random
$i = 0; // Set the iterator
do {
  echo $string; // Echo the text
  $i = $i + 1; // Increment the iterator
} while ($i < $x); // Test to make sure we didn't do it too many times
?>

This echos "Hello world!" anywhere from 1 to 10 times. You can search the PHP API to see how this works.