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
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!

Link to comment
Share on other sites

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';
}

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.