Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is a string comparison always returning false even for identical strings?
    primarykey
    data
    text
    <p>I know this is probably one of those "woods for the trees" questions but I've been sat here all day and I just cant see where Im going wrong.</p> <p>The Scenario:</p> <p>Basically I have a query returning the following assoc array of address results like so:</p> <pre><code>Array ( [0] =&gt; Array ( [Address_ID] =&gt; 12 [Address_String] =&gt; 23 Abledone road Hobs moat Solihull West Midlands B92 4JJ ) [1] =&gt; Array ( [Address_ID] =&gt; 13 [Address_String] =&gt; 22 Holdean Road Deepcut Surrey KT164NJ ) [2] =&gt; Array ( [Address_ID] =&gt; 14 [Address_String] =&gt; 32 Palbrough Road Nottingham N32 4NJ ) [3] =&gt; Array ( [Address_ID] =&gt; 15 [Address_String] =&gt; Flat 2B Gateway House Rutland Street Bearwood Westmidlands B66 9RT ) [4] =&gt; Array ( [Address_ID] =&gt; 16 [Address_String] =&gt; 258 Lincoln Street Bullworth Street Dundee DD35 9RJ ) [5] =&gt; Array ( [Address_ID] =&gt; 17 [Address_String] =&gt; 4 Scott Arms Avenue Beckton Hartfordshire H32 7JJ ) [6] =&gt; Array ( [Address_ID] =&gt; 18 [Address_String] =&gt; 14 Fairfield house Millbank road Bearwood Birmingham Westmidlands B137JJ ) [7] =&gt; Array ( [Address_ID] =&gt; 19 [Address_String] =&gt; 9 Scott road Olton Solihull Westmidlands B91 7TY ) [8] =&gt; Array ( [Address_ID] =&gt; 20 [Address_String] =&gt; Normandy House Bunnian Place Basingstoke RG21 7EJ ) [9] =&gt; Array ( [Address_ID] =&gt; 27 [Address_String] =&gt; 3 New Square Bedfont Lakes Feltham Middlesex TW14 8HB ) [10] =&gt; Array ( [Address_ID] =&gt; 28 [Address_String] =&gt; No. 1 The Square Temple Quay Bristol BS1 6DG ) [11] =&gt; Array ( [Address_ID] =&gt; 29 [Address_String] =&gt; The Visual Space Capital Park Fulbourn Cambridge CB21 5XH ) [12] =&gt; Array ( [Address_ID] =&gt; 40 [Address_String] =&gt; 21 St. Andrew Square Edinburgh Scotland CB21 5XH ) ) </code></pre> <p>And all I'm trying to do is match these results against a search string to see if the address is already in my db.</p> <p>my code is as follows:</p> <pre><code> public function addressExistsByString ($searchAddrString, $asJSON = FALSE) { if (!isset($searchAddrString)) { throw new Exception('The searchAddrString variable of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . 'was empty or set to NULL.'); } elseif (!is_string($searchAddrString)) { throw new Exception('The type of value for the argument addressID of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . ' was ' . gettype($searchAddrString) . ' a string was expected'); }if (!is_bool($asJSON)) { $asJSON = FALSE; } print_r ($addressArray = $this-&gt;getAllLocations(FALSE)); if ($addressArray === -1) { return -2; } else { $search = str_replace(',','', strtolower(trim($searchAddrString))); foreach ($addressArray as $record) { $recID = $record ['Address_ID']; $currentAddress =str_replace(',','', strtolower(trim($record['Address_String']))); echo 'Search address: '. $search. ' Current address: '. $currentAddress .' type of current address: '.gettype($searchAddrString).'&lt;br/&gt;'; if ($currentAddress === $search) { if ($asJSON) { echo json_encode($recID); } return intval($recID); } } if ($asJSON) { echo json_encode(-1); } return -1; } } </code></pre> <p>When I run this code I get the following output (sorry its a little long but the important bit is highlighted ):</p> <blockquote> <p>bunnian place basingstoke rg21 7ej Current address: 23 abledone road hobs moat solihull west midlands b92 4jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 22 holdean road deepcut surrey kt164nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 32 palbrough road nottingham n32 4nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: flat 2b gateway house rutland street bearwood westmidlands b66 9rt type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 258 lincoln street bullworth street dundee dd35 9rj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 4 scott arms avenue beckton hartfordshire h32 7jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 14 fairfield house millbank road bearwood birmingham westmidlands b137jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 9 scott road olton solihull westmidlands b91 7ty type of current address: string Search address: <strong>normandy house bunnian place basingstoke rg21 7ej</strong> Current address: <strong>normandy house bunnian place basingstoke rg21 7ej</strong> type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 3 new square bedfont lakes feltham middlesex tw14 8hb type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: no. 1 the square temple quay bristol bs1 6dg type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: the visual space capital park fulbourn cambridge cb21 5xh type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 21 st. andrew square edinburgh scotland cb21 5xh type of current address: string -1 </p> </blockquote> <p>The strings highlighted are exactly the same, I've tested them by taking them both and sticking them in a separate if statement as plain strings and true is always returned . I've tried ==, === casting, string values and everything else I can think of but for some reason -1 is always returned. I know its probably something very simple Im missing but I just cant see it, can anyone help?</p> <p>Thanks in advance. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload