...

View Full Version : Help checking if variables have the same value



cragllo
01-03-2006, 02:49 PM
Hi, I have six variables, From form data that a user enters, the data is numbers. I need to check if any are matching using an if statment, I how how to check two variables together
if($v1 == $v2){ But would like to know how to check six variables at once, to see if any are matching.

Masterslave
01-03-2006, 02:59 PM
I think you can use switch.
http://nl2.php.net/switch

missing-score
01-03-2006, 06:43 PM
So you have 6 variables and you want to see if ANY of them match up with ANY other?

I dont know of any built in method to do this (although I may be wrong). I wrote this small function that will check if any of the inputted values are the same and if so, return a "position identifier" so you have an idea which ones it were:


function checkEquality(){

$arrayValues = array();

for( $i = 0; $i < func_num_args(); $i++ ){
$arrayValues[] = func_get_arg($i);
}

$counter = 1;
while( current( $arrayValues ) ){

$arrayKey = key( $arrayValues );
$thisValue = array_shift( $arrayValues );

if( false !== ( $keyEnd = array_search( $thisValue, $arrayValues ) ) ){

return array( $arrayKey+($counter++), $keyEnd+$counter );

}

}

return false;

}

and it would be used like so:



if( false != ( $eq = checkEquality( 'value1', 'value2', 'value3', 'value2' ) ) ){
echo "Variable at position {$eq[0]} is equal to that at position {$eq[1]}";
} else {
echo 'Nothing matches';
}


This function will only give you the FIRST set of matching values.l

cragllo
01-03-2006, 07:03 PM
Thanks for that! Just what I needed. This will help me greatly :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum