Generate random numbers
  1. Create a new script with this code:

    <?php

    $random_number = rand(1,10);

    print "<p>Your Lucky Number from 1 to 10 is $random_number.</p>\n";

    print "Click the Reload button on your browser to get a new random number.";

    ?>
  2. Save the script as random.php in the PHPSCRIPTS folder.

    Here's what the relevant lines in this script do:
  • $random_number = rand(1,10);

    The rand() command generates a random number from 1 to 10.

    It is then assigned to the variable $random_number.

  • print "<p>Your Lucky Number from 1 to 10 is $random_number .</p>\n";

    Prints "Your Lucky Number from 1 to 10 is 8."

    Because it is a random number, the number on your screen will be different.

Want ad-free tutorials like these for classroom use? Get them at Visilearn.com.