Problem with $ _POST and IF

1

I am trying to do if in case 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";
    }
    
asked by anonymous 23.03.2018 / 00:30

1 answer

10

To know if one text is contained in another, use strpos .

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

Important: Note the use of !== false , because if string is at the beginning, using only != will go wrong, because strpos will be zero, indicating that there is a 12 , but at the beginning of the string , since the count starts at the zero position.

If you want the 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, due to the significant performance difference.

More details in the manual:

  

link


Recommended reading on% difference from% to === to !== and == :

  

What is a loose comparison?

    
23.03.2018 / 00:44