Kevin
Kevin

Reputation: 33

PHP variables look the same but are not equal (I'm confused)

OK, so I shave my head, but if I had hair I wouldn't need a razor because I'd have torn it all out tonight. It's gone 3am and what looked like a simple solution at 00:30 has become far from it.

Please see the code extract below..

$psusername = substr($list[$count],16);

if ($psusername == $psu_value){
$answer = "YES";
}
else {
$answer = "NO";
}

$psusername holds the value "normann" which is taken from a URL in a text based file (url.db)

$psu_value also holds the value "normann" which is retrieved from a cookie set on the user's computer (or a parameter in the browser address bar - URL).

However, and I'm sure you can guess my problem, the variable $answer contains "NO" from the test above.

All the PHP I know I've picked up from Google searches and you guys here, so I'm no expert, which is perhaps evident.

Maybe this is a schoolboy error, but I cannot figure out what I'm doing wrong. My assumption is that the data types differ. Ultimately, I want to compare the two variables and have a TRUE result when they contain the same information (i.e normann = normann).

So if you very clever fellows can point out why two variables echo what appears to be the same information but are in fact different, it'd be a very useful lesson for me and make my users very happy.

Upvotes: 1

Views: 1030

Answers (5)

Kevin
Kevin

Reputation: 33

I just want to thank all who responded. I realised after viewing my logfile the outputs in HEX format that it was the carriage return values causing the variables to mismatch and a I mentioned was able to resolve (trim) with the following code..

$psusername = preg_replace("/[^[:alnum:]]/u", '', $psusername);

I also know that the system within which the profiles and usernames are created allow both upper and lower case values to match, so I took the precaution of building that functionality into my code as an added measure of completeness.

And I'm happy to say, the code functions perfectly now.

Once again, thanks for your responses and suggestions.

Upvotes: 0

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

0D 0A is a new line. The first is the carriage return (CR) character and the second is the new line (NL) character. They are also known as \r and \n.

You can just trim it off using trim().

$psusername = trim($psusername);

Or if it only occurs at the end of the string then rtrim() would do the job:

$psusername = rtrim($psusername);

If you are getting the values from the file using file() then you can pass FILE_IGNORE_NEW_LINES as the second argument, and that will remove the new line:

$contents = file('url.db', FILE_IGNORE_NEW_LINES);

Upvotes: 1

David White
David White

Reputation: 1873

Since it looks like you have non-printable characters in your string, you can strip them out before the comparison. This will remove whatever is not printable in your character set:

$psusername = preg_replace("/[[:^print:]]/", "", $psusername);

Upvotes: 0

RDev
RDev

Reputation: 1281

Since i can't see what data is stored in the array $list (and the index $count), I cannot suggest a full solution to yuor problem. But i can suggest you to insert this code right before the if statement:

var_dump($psusername);
var_dump($psu_value);

and see why the two variables are not identical. The var_dump function will output the content stored in the variable and the type (string, integer, array ec..), so you will figure out why the if statement is returning false

Upvotes: 0

Kyle
Kyle

Reputation: 403

Do they echo the same thing when you do:

echo gettype($psusername) . '\n' . gettype($psu_value);

Upvotes: 1

Related Questions