PHP Match a string exactly

38,541

Solution 1

You need to use == not just =

$check = 'this is a string 111';
if ($check == 'this is a string') {
echo 'perfect match';
} else {
echo 'it did not match up';
}

= will assign the variable.

== will do a loose comparison

=== will do a strict comparison

See comparison operators for more information.

Solution 2

For equality comparison you want the == operator. = is assignment.

if ($check = 'this is a string') {

should be

if ($check == 'this is a string') {

Don't worry, we've all done it. I still do :)

Solution 3

the == comparison operator will work in most cases, but fails to do an exact match in some edge cases*.

Using === operator is best.

if ($check === 'this is a string') {

An example where == works unexpectedly

$check = '2';
if ($check == '          2') {

Solution 4

if ($check = 'this is a string') assigns the string to $check variable which is always defined and thus, returns always true in the if

should be if ($check == 'this is a string')

Solution 5

You're using the assignment operator, =, instead of the equality operator ==.

You need to use

if ($check == 'this is a string') {
Share:
38,541
C0nw0nk
Author by

C0nw0nk

Updated on January 21, 2020

Comments

  • C0nw0nk
    C0nw0nk over 4 years
    $check = 'this is a string 111';
    if ($check = 'this is a string') {
    echo 'perfect match';
    } else {
    echo 'it did not match up';
    }
    

    But it returns perfect match everytime instead of it did not match up... I can not seem to get the string to match the case exactly it will only work if part of the string matches up.

    If i try to complicate things a little using board code and regex patterns it becomes a nightmare.

    if ($check = '/\[quote(.*?)\](.*?)\[\/quote\]/su') {
    $spam['spam'] = true;
    $spam['error'] .= 'Spam post quote.<br />';
    }
    

    So if the post only contained quote tags it would be considered spam and ditched but i can not seem to solve it perhaps my patterns are wrong.

  • DaveRandom
    DaveRandom over 12 years
    which is always defined and thus, returns always true - not entirely true. If you do if ($check = 0) it will evaluate to false. What will actually happen here is that PHP will evaluate the right side of the expression as boolean.
  • Mr. Llama
    Mr. Llama over 12 years
    If you really want to be exact, use the strict comparison (===) operator, otherwise the following is true: if ('123' == 123)
  • dweeves
    dweeves over 12 years
    exact, but as long as you assign a non empty string,it'll evaluate to a true equivalent.
  • C0nw0nk
    C0nw0nk over 12 years
    Thanks it did work for my basic string but not when i start complicating things with regex patterns
  • DaveRandom
    DaveRandom over 12 years
    ...unless the string is '0'
  • C0nw0nk
    C0nw0nk over 12 years
    Thanks it did work for my basic string but not when i start complicating things with regex patterns
  • Nick
    Nick over 12 years
    Yeah, string comparisons won't work using regex...for that you'd need to do a preg_match - php.net/manual/en/function.preg-match.php