1. Computing
Send to a Friend via Email

Create a program that...

By

This is a PHP quiz yourself challenge. What that means is that I will pose a question, similar to the type of question you might get in a beginning programming class. You will be asked to create a simple program, not necessarily the most useful thing, but something that will make you write some code. After all, actually writing the code teaches you a lot. If your program works you pass, there is more than one correct answer to each problem... but if you want to see my solution, or if you can’t get yours to work, the answer will be at the end of the article.

The challenge: Create a simple program that returns to the screen ten random numbers between 1 and 50. If the number is greater than 25 the program should also include the word “High” and if the number is less than 25 the program should also include the word “Low”. If it is exactly 25 nothing else is included next to the number on the list.

Do not read the answer below until you have attempted the code on your own!

No cheating!

The answer:

<?php 

$num = 1; 

while ( $num <=10 ) 

{ 
$x = rand(1,50);

if ($x > 25)
{
Echo $x." High<br>";
}

elseif ($x < 25)
{
Echo $x." Low<br>";
}

Else
{
Echo $x."<br>";
}


$num++; 

} 
?> 

As I mentioned above there are certainly other ways to achieve the same output for the program, and this is just one example. Let’s look more closely at what is being done to achieve the goal in this code.

One thing you will see that we used is a while loop. In this loop we assign a number to 1, and continue to loop until we go above ten. At the end of each loop we add one to the number, so that the middle code will run exactly ten times.

Inside of our while loop we first create a random number. By putting the 1 and 50 inside the rand () brackets, we are setting the parameters for the random number to be between 1 and 50. We then use the conditionals if, elseif, and else. These conditional statements put the random number we choose through a series of tests. First we check if the number is greater than 25. If this is true, then we echo the number, and the word “High”. We also put in some HTML breaks so our list is formatted nicely. Next we check if the number is less than 25. If this is the case we echo the number with the word “Low” again including some HTML breaks. If neither of these is true, then the only number that it could be is 25 so we simply echo the number with no words, but do include a line break.

At the end of our conditional statement, we remember to include the $num++ to indicate to the program to add one to the $num variable and return to the beginning of the loop.

This program could also have been written using switch instead of if and else. Can you rewrite your program to use a switch loop? You might also use Print instead of Echo. Challenge yourself and see how many different ways you can write this same program. If you are learning another language, such as C++, see if you can then take your PHP program and write it in that language. You will see that most of the same concepts will carry over, even if the syntax isn't exactly the same.

If you want to share the code you came up with as a solution to this problem, you can leave a comment on the blog post related to this article, or you can visit the PHP forums on About.com. Happy Programming!

©2014 About.com. All rights reserved.