简体   繁体   中英

Comparing two identical strings returns false in PHP

This one is completely beyond me. 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". Can anyone shed any light on this?

if ($this->data['list_text']) { //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++;
            }
        }

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. Eg the actual type. And the length of strings.

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

Try to check encoding of both string compared.

maybe it is utf8 compared with iso-5589-1 with some weird chars.

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

尝试trim()用于任何空格以及var_dump()以查看是否有其他任何东西与它一起传递。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM