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";
}
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";
}
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:
Recommended reading on the difference of ===
and !==
for ==
and !=
:
I did not understand the negative vote
Browser other questions tagged php if
You are not signed in. Login or sign up in order to post.
This syntax you used does not exist. Search for
strpos
.– Woss
In addition to the above, as I typed 12 if the comparison is of difference??
– Don't Panic
I put wrong this part was you n typed 12 kkkk
– romulo henrique
obg @Andersoncarloswoss
– romulo henrique