6

I'm trying to compare two strings. When I echo them, they appear to be identical, yet when I compare them with the '==' operator, it returns false. For example, when running the code below on my database. It outputs things like "APPARENTLY Apple does not equal Apple". What is the reason?

if ($this->data['list_text']) { // The user has entered into textarea

    $list = nl2br($this->data['list_text']);

    $list_array = explode('<br />', $list);

    $ranking = 1;
    $company_array = $this->CompanyList->CompanyRanking->Company->find('list', null);

    // This is the comparison bit
    foreach ($list_array as $key => $value) {
        $companyId = null;
        foreach ($company_array as $key2 => $value2) {
            if ($value2 != $value) {
                echo 'APPARENTLY ' . $value2 . ' does not equal ' . $value;
            } else {
                $companyId = $key2;
                break;
            }
        }

        $this->data['CompanyRanking'][$ranking]['ranking'] = $ranking;
        $this->data['CompanyRanking'][$ranking]['company_id'] = $companyId;
        $ranking++;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gomezuk
  • 299
  • 6
  • 15
  • A canonical is *[String comparison using '==' vs. 'strcmp()'](https://stackoverflow.com/questions/3333353/)* (the answers also cover `===`). – Peter Mortensen Feb 02 '22 at 00:04

5 Answers5

11

Try var_dump() instead of echo.

echo 'APPARENTLY '.$value2.' does not equal '.$value;   
echo '<pre>Debug: ';
echo 'value='; var_dump($value);
echo 'value2='; var_dump($value2);
echo '</pre>';

It provides additional information. E.g. the actual type. And the length of strings.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 1
    Especially useful when using var_dump is to make sure you have xdebug installed, as it will pretty up the output (things can be hard to read with the traditional echo, or, forbid, print_r). – Travis Leleu Oct 14 '09 at 15:34
4

Do the strings have any extra whitespace you're not seeing? Try trimming them.

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
  • In a similar vein, try using `echo 'APPARENTLY >'.$value2.'< does not equal >'.$value.'<';` which will also illustrate this better. – Matthew Scharley Oct 14 '09 at 11:57
  • Thanks for the suggestion. I tried adding "if (trim($value2) != trim($value))" and I'm still getting "APPARENTLY Apple does not equal Apple". – gomezuk Oct 14 '09 at 11:57
  • On another note, my code posted above would be faster as you do not perform the first trim on $value for every iteration of $company_array. – Corey Ballou Oct 14 '09 at 12:04
  • just trim() is probably not enough because it will only trim whitespace at the beginning and the end of the strings. – markus Oct 14 '09 at 12:11
4

Try to check the encoding of both strings compared.

Maybe it is UTF-8 compared with ISO 8859-1 with some weird characters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ismael
  • 2,330
  • 1
  • 25
  • 37
3

I agree with Olafur. I removed trim and replaced it with a preg_replace due to the fact you are assuming $value and $value2 are companyIDs. You can make a quick modification on these if the companyID is supposed to be alphanumeric, contain hyphens, etc... This version should do it:

if ($this->data['list_text']) { 
    $list = nl2br($this->data['list_text']);
    $list_array = explode('<br />', $list);

    $ranking = 1;
    $company_array = $this->CompanyList->CompanyRanking->Company->find('list',null);

    foreach ($list_array as $key => $value) {

        // remove any non digit characters
        $value = preg_replace('/[^0-9]/i','', $value); 
        $companyId = null;

        foreach ($company_array as $key2 => $value2) {

            // remove any non digit characters
            $value2 = preg_replace('/[^0-9]/i','', $value2); 

            if ($value2 != $value) {
                echo 'values not equal';                
            } else {
                $companyId = $key2;
                break;
            }
        }

        $this->data['CompanyRanking'][$ranking]['ranking'] = $ranking;
        $this->data['CompanyRanking'][$ranking]['company_id'] = $companyId;
        $ranking++;
    }
}
Corey Ballou
  • 42,389
  • 8
  • 62
  • 75
  • Many thanks cballou. It turns out the trim() was working, but your code was much more efficient than mine. – gomezuk Oct 14 '09 at 12:11
3

Try trim() for any white space as well as var_dump() to see if anything else is being passed with it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383