Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help checking if variables have the same value

    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
    PHP Code:
    if($v1 == $v2){ 
    But would like to know how to check six variables at once, to see if any are matching.

  • #2
    Regular Coder Masterslave's Avatar
    Join Date
    Dec 2005
    Posts
    287
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I think you can use switch.
    http://nl2.php.net/switch

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    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:

    PHP Code:
    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

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that! Just what I needed. This will help me greatly


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •