Problem with strpos function

I use the strpos function to see if I should send an email to the user or not. If the array POST contains a specific word in a form field and another string does not contain it, I should send an email, otherwise I should move on and do nothing:

$my_string = 'word';
$email = '';
if ((strpos($_POST['field'],'word') !== FALSE || strpos($_POST['field'],'WORD') !== FALSE) &&
	(strpos($my_string,'word') === FALSE || strpos($my_string,'WORD') === FALSE)) {
	$subject = 'Email subject';
	$message = 'Email message';

In this particular case, $my_string contains ‘word’, therefore the email should not be sent, but it’s sent anyways. What am I doing wrong?

Doesn’t answer your question, but you might want to check out

stripos is case insensitive

$_POST['field'] =" string with a WORD in it";


echo 'send it';


echo 'dont send it';


This works because stripos() returns the position of the match, which when tested in a boolean fashion returns true.

if() works by applying a test to see if something equates to true.

true is true so echo ‘send it’;

If not found then stripos returns false, false it not true so the if() failure evokes the else {}

I didn’t know that there was a function named stripos, I thought that there was only strpos :smiley:

Just one thing: writing only


didn’t work, I had to write

if(stripos($_POST['field'],'word') !== false)

to make it work. Just in case somebody needs to do the same thing :slight_smile: