Compare two sets of numbers to see if they match

Learning PHP one step at a time so I’m staying away from Arrays until I figure this part out first.

This is like a lottery so I want to get random numbers:

$num1 = rand(1,30);
$num2 = rand(1,30);
$num3 = rand(1,30);
$num4 = rand(1,30);
$num5 = rand(1,30);
$pwrBallPick = rand(1,20);

and I want to compare them to the lottery numbers below to see if there is a match:

$x1 = 10;
$x2 = 29;
$x3 = 18;
$x4 = 28;
$x5 = 4;
$PowerBall = 15

How would I set this up so that I can print what the person won?

if the count is 5 and the power ball is a match they won one hundred dollars.
else if count is 5 they won one million dollars
else if count is 4 and power ball is match they won ten thousand dollars.
etc

or am I going in the wrong direction?

Does $num1 always only refer to $x1, num2 to x2 and so forth OR are you trying to see if $num1 matches $x1 or x2 and so on?

Have you looked at the Php Manual on Comparison Operators?
http://www.php.net/manual/en/language.operators.comparison.php

$num1 is a random number that the user is checking against any of the $x1 numbers to see if they won.

Ex: 5 matching numbers plus the powerball wins 247,000,000
5 matching numbers and no powerball wins 1,000,000
4 matching numbers plus the powerball wins 10,000
4 matching numbers and no powerball wins 100.00
3 matching numbers plus the powerball wins 100.00
3 matching numbers and no powerball wins 7.00
2 matching numbers plus the powerball wins 7.00
1 matching number plus the powerball wins 4.00
powerball only wins 4.00

Thanks for your help!

Should the numbers be unique? With your current setup the winning combination might actually be 1, 1, 1, 1, 1 + 1

I know you wanted a non array solution, but play with this, you just might learn something new. This is only the number generation part which will get you half way to your goal. Now you just need to do your compares. I will see about posting something on the comparing part and see if you can put the two together. You will learn a lot better than if I just did it all for you. If you get stuck or have questions feel free to ask. JimL is knowledgeable and can provide alternate ideas to the same problem which is good for all of us.

[php]$number_generator = range(1, 30);

$keys = array_rand($number_generator, 5);

foreach ($keys as $key => $value)
{
echo $number_generator[$value] . ', ';
}
$powerball = array_rand($number_generator);

echo “
Powerball number: $number_generator[$powerball].”;[/php]

  • The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

Alternate Number Generator

[php]$a = range(1, 30);
$random_keys=array_rand($a,5);
echo $a[$random_keys[0]]."
";
echo $a[$random_keys[1]]."
";
echo $a[$random_keys[2]]."
";
echo $a[$random_keys[3]]."
";
echo $a[$random_keys[4]];[/php]

Thanks but this is way over my head. I really need to wrap my brain around the simpler way and then move up to arrays. I know it will be a lot longer path but one step at a time so I can get this. I really want to learn and hoping someone will show me in a way where everything will start clicking.

I already have the random numbers generating and need to zero in on how to compare the two sets to see if there’s a win. Thank you for your patience with me. I think once I do it the long way I will appreciate arrays when I get to it!

You can compare values with ==

So you’re looking for something like: if ($a1 == $a2) etc

You will be going for a long walk down code lane so to speak. The following code only covers comparing $num1 but what your asking for is this:

[php]if ($num1 == $x1)
{
//Do something
}
if ($num1 == $x2)
{
//Do something
}
if ($num1 == $x3)
{
//Do something
}
if ($num1 == $x4)
{
//Do something
}
if ($num1 == $x5)
{
//Do something
}
if ($num1 == $PowerBall)
{
//Do something
}[/php]

You will have to add six times the amount of this code to get the complete version of what you want.

Let me help you in real life with Arrays. You actually are already creating and using them and didn’t realize it.

An array is basically just a list. Each item on your list is a value.

In real life you have probably wrote down a shopping list to go to the market. That list is your array.
Everything you write on it is a value in your shopping list array. Milk, Bread, Eggs, Cereal.

So you have a blank shopping list before you write anything down. You have an empty array or an empty list
[php]$shoppingList = array();[/php]

You look in the fridge and your out of milk and orange juice. Put it on the list. In php you can do it in several ways. This is an INDEXED ARRAY. The Zero and one is the index.
[php]$shoppingList [0]=“Milk”;
$shoppingList [1]=“Orange Juice”;[/php]

OR you can use a builtin PHP array function array_push

[php]array_push($shoppingList,“Milk”,“Orange Juice”);[/php]

Oops, we forgot grape juice, write it down
[php]array_push($shoppingList,“Grape Juice”);[/php]

Your short on cash, so you want to only get 5 items, so you count how many things you wrote on your shopping list.

[php]echo count($shoppingList);// We have 3 items[/php]

You decide you dont want the grape juice so you scratch the last item off your list
[php]array_pop($shoppingList);// Grape Juice, the last thing you wrote is off the list[/php]

Since your well organized and dont want to spend a lot of time at the market, you write down your grocery list grouping them by area of the market so you dont have to go back and forth around the store.

Meat Dept: Steak, Hamburger, Ribs
Dairy Isle : Milk, Orange Juice, Eggs
Bakery Dept: Bread, Rolls, Donuts

You have now created a Multidimensional Array and you didnt even know it. A multidimensional array is an array containing one or more arrays.

[php]$shoppingList = array
(
“Meat Dept” =>array (“Steak”, “Hamburger”, “Ribs”),
“Dairy Isle” =>array (“Milk”, “Orange Juice”, “Eggs”),
“Bakery Dept” =>array (“Bread”, “Rolls”, “Donuts” )
);[/php]

There is much more but hopefully you can relate something you have been doing all your life to PHP Arrays. It’s Just a list that you can do many things with. Search, Sort, Add to, delete from, …

@benanamen Your description of Arrays is very helpful and I will work on arrays as soon as I get this Lottery working. It doesn’t seem so intimidating now, I like the way you described it to me. Thank you.

The lottery is not printing the correct information. For example it should print something every time I hit refresh but sometimes it will say you won 4.00 or “Your a loser” or nothing at all! I have been looking but can’t find the mistake, did I set up the print correctly?

I’m going to post the code separately, trying to upload it correctly. I read I’m doing it wrong.

[php]<?php

$num1 = rand(1,20);
$num2 = rand(1,20);
$num3 = rand(1,20);
$num4 = rand(1,20);
$num5 = rand(1,20);
$pwrBall = rand(1,15);

$win1 = 5;
$win2 = 10;
$win3 = 4;
$win4 = 17;
$win5 = 1;
$winpwrBall = 15;

$pwrMatch;
$match1;
$match2;
$match3;
$match4;
$match5;
$match6;

if ($num1 == $win1)
{
$match1 = 1;
}
if ($num1 == $win2)
{
$match1 = 1;
}
if ($num1 == $win3)
{
$match1 = 1;
}
if ($num1 == $win4)
{
$match1 = 1;
}
if ($num1 == $win5)
{
$match1 = 1;
}
else {
$match1 = 0;
} //end match1

if ($num2 == $win1)
{
$match2 = 1;
}

if ($num2 == $win2)
{
$match2 = 1;
}
if ($num2 == $win3)
{
$match2 = 1;
}
if ($num2 == $win4)
{
$match2 = 1;
}
if ($num2 == $win5)
{
$match2 = 1;
}
else {
$match2 = 0;
} //end match2

if ($num3 == $win1)
{
$match3 = 1;
}

if ($num3 == $win2)
{
$match3 = 1;
}
if ($num3 == $win3)
{
$match3 = 1;
}
if ($num3 == $win4)
{
$match3 = 1;
}
if ($num3 == $win5)
{
$match3 = 1;
}
else {
$match3 = 0;
} //end match3

if ($num4 == $win1)
{
$match4 = 1;
}

if ($num4 == $win2)
{
$match4 = 1;
}
if ($num4 == $win3)
{
$match4 = 1;
}
if ($num4 == $win4)
{
$match4 = 1;
}
if ($num4 == $win5)
{
$match4 = 1;
}
else {
$match4 = 0;
} //end match4

if ($num5 == $win1)
{
$match5 = 1;
}

if ($num5 == $win2)
{
$match5 = 1;
}
if ($num5 == $win3)
{
$match5 = 1;
}
if ($num5 == $win4)
{
$match5 = 1;
}
if ($num5 == $win5)
{
$match5 = 1;
}
else {
$match5 = 0;
} //end match5

if ($pwrBall == $winpwrBall)
{
$match6 = 1;
}
else {
$match6 = 0;
} //end match6

if ($match1 == 1 && $match2 == 1 && $match3 == 1 && $match4 == 1 && $match5 == 1 && $match6 == 1){
print “You won 330 million Dollars!
”;
}

if ($match1 == 1 && $match2 == 1 && $match3 == 1 && $match4 == 1 && $match5 == 1 && $match6 == 0){
print “You won $1,000,000!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 4) && ($match6 == 1)){
print “You won $10,000!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 4) && ($match6 == 0)){
print “You won $100.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 3) && ($match6 == 1)){
print “You won $100.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 3) && ($match6 == 0)){
print “You won $7.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 2) && ($match6 == 1)){
print “You won $7.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 1) && ($match6 == 1)){
print “You won $4.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 0) && ($match6 == 1)){
print “You won $4.00!
”;
}

if (($match1 + $match2 + $match3 + $match4 + $match5 == 0) && ($match6 == 0)){
print “Your a loser!
”;
}

 print "Your numbers are: $num1, $num2, $num3, $num4, $num5. Your PowerBall number is: $pwrBall <br />
     This weeks numbers are: 5, 10, 4, 17, 1 with the PowerBall number of 15.<br />"

?>[/php]

Still not printing each win correctly and I’ve tested every one separately and separately it will display the correct print but when I put them together it may or may not print a loss and if it does print it will only display “You won 4.00” even if they won $7.00.

Why is this happening?

Sponsor our Newsletter | Privacy Policy | Terms of Service