Problem with $_POST and IF

Asked

Viewed 52 times

1

I am trying to do q if the person type 12 in any part of the text he enters the if but he is not entering

if ($_POST['mensagem'] !="%12%"){
        echo"vc digitou 12";
    }
  • 3

    This syntax you used does not exist. Search for strpos.

  • In addition to the above, as I typed 12 if the comparison is of difference??

  • I put wrong this part was you n typed 12 kkkk

  • obg @Andersoncarloswoss

1 answer

10


To know if one text is contained in another is used strpos.

if ( strpos( $_POST['mensagem'] , '12' ) !== false ){
    echo 'vc digitou 12';
}

Important: note the use of !== false, for if the string is in the beginning, use only != will go wrong because the result of strpos shall be zero, indicating that there is a 12, but at the beginning of string, as the count starts at zero position.

If you want reverse behavior:

if ( strpos( $_POST['mensagem'] , '12' ) === false ){
    echo 'vc NAO digitou 12';
}

Use === for the same reason explained above.

Note that there are other functions to find substrings, but the manual itself recommends strpos for these cases, by the significant difference in performance.

More details in the manual:

https://secure.php.net/manual/en/function.strpos.php


Recommended reading on the difference of === and !== for == and !=:

What’s a loose comparison?

  • 1

    I did not understand the negative vote

Browser other questions tagged

You are not signed in. Login or sign up in order to post.