Jump to content

Comparing two numbers


thekauz

Recommended Posts

Hey I have a quick question.

I am trying to have a part of this PHP script compare two numbers.

There is a randomly generated two-digit number named $random and

There is another two digit number named $number.

I want to make it so the PHP checks to see if $number has any of the same charcters as $random.

I don't know how to do this though. I suspect that somehow both numbers need to be broken apart somehow.

Any advice would be appreciated.

Let me know if I should explain it more clearly :)

Thanks a ton!

Link to comment
https://forums.phpfreaks.com/topic/222602-comparing-two-numbers/
Share on other sites

I want to have a user input a two-digit number in a form, then the number is passed onto a php page where another two-digit number is generated.

I have made this all, but I am having trouble with the part where the two numbers are compared.

What I want to have happen is this:

 

If the two two-digit numbers share any of the same characters, then it will say "shared" (ie 18 & 17...or 45 & 57)

If they share both the same characters, but the characters are in the wrong places (ie 28 & 82), then it will say "shared shared"

If they don't share anything, then it will say "nothing"  (ie 28 & 17)

If they both have the same characters in the same places, then it will say "perfect" (ie 11 & 11)

 

I really do hope this clears up any confusion.

I hope somebody can help me through with this problem.

Thanks a ton!

One-liner:

$a = "12"; $b = "92";
if (strlen(count_chars($a . $b, 3)) < 4) {
    // one or more repeated numbers
} else {
    // four unique digits
}

 

That won't work if one of the numbers is for example 11, 22, 33, 44 and so on, it will say a number is repeated between the two variables when it isn't.

 

This will do it, and it will work on  numbers or strings of any length:

 

<?php

$a = 32;
$b = 83;

$found = false;

for($i=0; $i<strlen($a); $i++)
{
$num = substr($a, $i, 1);

if(strpos($b, $num) !== false)
{
	$found = true;
	break;
}
}

if($found)
{
echo 'A charcter exists in both variables';
}
else
{
echo 'None of the variables share a character';
}

?>

Thanks I read about all of the functions you used and I understand how this works!

That's just what I needed!

But I would like to know still how I would handle the following situation:

 

When

$a = 28

$b = 82

How would I make it so it says "shared shared" since there are two shared characters?

 

Does that question make sense?

Your time is greatly appreciated thank you so much!

In that case:

 

<?php

$a = 32;
$b = 23;

$found = 0;

for($i=0; $i<strlen($a); $i++)
{
$num = substr($a, $i, 1);

if(strpos($b, $num) !== false)
{
	$found++;
}
}

if($found > 0)
{
// $found contains the amount of shared characters, so here just loop that amount and echo 'shared'
for($i=0; $i<$found; $i++)
{
	echo 'shared ';
}
}
else
{
echo 'None of the variables share a character';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.