0

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)

6
  • 3
    strcasecmp() returns 0 when the strings are equal. Commented Apr 10, 2019 at 12:04
  • It returns a positive number when the first string is higher than the second string, it returns a negative number when the first string is lower. Commented Apr 10, 2019 at 12:04
  • Hi Barmar, what do you mean by higher sorry? The second piece of code isn't being reached either. else if(strcasecmp($location_name_chk, $comparison_string) == 0) { echo "Location is Transfers"; } Commented Apr 10, 2019 at 12:08
  • He's saying change your test to == 0 instead of == 1. When the strings are the same you'll get 0 returned. Commented Apr 10, 2019 at 12:09
  • 1
    It's like dictionary order. If the word would be later in the dictionary, it's higher. Commented Apr 10, 2019 at 12:11

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.