[php home]

Php_6: Using Conditionals, Operators, Random Numbers & Images

PHP can select a random number by using the rand() function.

Create the following simple php page. Save as 6_1.php. In this example, I have the action going back to itself. You could simply hit the refresh button in your browser to get the same result.

<html>
<head><title>random numbers</title></head>
<body>
<form action="6_1.php" method="post">
<input type="submit">
</form>

<p>

<?php
$num = rand(1,6);
echo "you picked number $num";
?>

</body>
</html>

The rand(1,6) function will randomly select a number between (including) 1 and 6.

Exercise 6: When you follow this NBA Logos link, you will find a number of logos. Set up a page that will randomly select an NBA team, display a team greeting and also display the team logo. Select 6 teams.

To display an image, simply put the regular html tag inside the echo " " tag.

echo "<img src=\"graphics/pict.gif\">";

Hint: Rename your logos so that you can take advantage of the random number selected. (logo1.gif)