Jump to content

Need help with correct syntax for "if" condition


capella07

Recommended Posts

I'm trying to determine if a string is in a string, and if it is not, then do what's in the brackets.

 

Example:

I want to do something if the string "mobile" is not in the string "automobile". I know that strpos returns False if the substring isn't found in the main string, so I currently have something like the following in my code:

 

$bigstring = "automobile";
$littlestring = "mobile";
if( !(strpos($bigstring, $littlestring) ) {
// Do something
}

 

But it didn't seem right with the exclamation mark where it is.

 

If that's not the right way to do what I want to do, could someone provide an example of the right way?

 

Thanks in advance.

Actually, if LittleString is at the beginning of BigString, then strpos() will return zero which will evaluate to false.  So you need to check for equivalence:

 

if(strpos($bigstring, $littlestring) === false) {
// Do something

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.