foggy839_33
foggy839_33

Reputation: 53

PHP String Comparison Issue

I can not figure out where I'm going wrong.

I've done so many string comparisons before and this one just refuses to work, the code is below;

$comparison_string = 'Transfers';

if ($location_name_chk == 0)
    $location_name_chk = 'Not_Transfers_Location';

echo $location_name_chk;
if(strcasecmp($location_name_chk, $comparison_string) == 1) {
    echo 'Location not Transfers';
    die();

Essentially, it's not getting to the part where it's comparing the strings.

I can 100% confirm that $comparison string == Transfers and that $location_name_chk == Not_Transfers_Location as the echo $location_name_chk does show correctly in the Network Display tab.

Any ideas? I have tried a range of different ideas that are in other questions, but to no avail (trim, strcasecmp etc)

Upvotes: 0

Views: 127

Answers (1)

Cid
Cid

Reputation: 15247

Sometime (in fact, always), reading the manual helps a lot.

strcasecmp ( string $str1 , string $str2 ) : int

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Some examples :

strcasecmp("abc", "Abc"); // 0

strcasecmp("abc", "Abcd"); // negative number (-1)

strcasecmp("abc", "Abcdefg"); // negative number (-4)

strcasecmp("Abcd", "abc"); // positive number (1)

strcasecmp("Abcdefg", "abc"); // positive number (4)

strcasecmp("abc", "def"); // negative number (-3)

strcasecmp("def", "abc"); // positive number (3)

In your code, change this :

if(strcasecmp($location_name_chk, $comparison_string) == 1)

To this

//     Notice this --------------------------------------V
if(strcasecmp($location_name_chk, $comparison_string) == 0)

Upvotes: 1

Related Questions