If else statement and strcasecmp

I am trying to compare 2 strings (both alphabets) and run a script depending on if the condition is true or false. Both strings are stored as text on the server and pulled out from different database tables for comparison. The result of the output is displayed at the fron end for the end user.
Take a look at these 2 methods.
a.

    if (strcasecmp($str1, $str2) == 0) {
          echo 'Both match';
    } else {
        echo 'Do not match';
    }

This failed to run. I keep getting ‘Do not match’ even when both strings are same

b.
Here I removed the else statement and provided an alternative variable.

    $output = 'Do not match';
    if (strcasecmp($str1, $str2) == 0) {
        echo 'Both match';
    }

This works when both strings are same or different.
Why is this so?

B does not make sense. You make a variabele $output which you never use anymore.

Should be something like

$output = ‘Do not match’;
if (strcasecmp($str1, $str2) == 0) {
    $output =  ‘Both match’;
}

echo $output;

So my question would be are they truly the same?

$str1 = "we are the world";
$str2 = "We are the World";

    if (strcasecmp($str1, $str2) == 0) {
          echo 'Both match';
    } else {
        echo 'Do not match';
    }
// Both match

But,

$str1 = "we are the world";
$str2 = "We are the World ";

    if (strcasecmp($str1, $str2) == 0) {
          echo 'Both match';
    } else {
        echo 'Do not match';
    }
/// Do not match

You can probably write a query that does whatever it is you are trying to accomplish.

What does using var_dump() on both strings show?

Thank you all for your input. Your comments woke up by sleeping brain.
I have found out my error. My narative didn’t give the full picture
Yes method (a) makes sense. However it does not work for this case because I am looking for a string in a given set of arrays using a loop function. This string can only appear once and in one array only. This is where method (b) makes sense and it works. If the string is found, the output will be Both match, But if not found in all the arrays, the output will be Do not match.
If else is added, when the string is found, next loop will reset output to Do not match. except the last array provides the matched string (and this condition cannot be guaranteed). The correct full script is:

    $output = 'Do not match';
    for ($i=0; $i<$count; $i++) {
      if (strcasecmp($str, $arr[$i][2]) == 0) {
        $output = 'Both match';
      }
    }
    echo $output;

You should probably tell us about the real problem you are trying to solve by doing this. What is the high level overview of what you have going on?

Ignoring that you should just do this in a query, the following will execute several times faster, since it is operating on the data as a set using php array functions -

if(in_array($str,array_column($arr,2)))
{
    echo 'found';
} else {
    echo 'not found';
}

Edit: and if the match needs to be case-insensitive, throw in an array_map() using ‘strtolower’ as the call-back function.

1 Like

^
That!

Sponsor our Newsletter | Privacy Policy | Terms of Service