capella07 Posted April 21, 2010 Share Posted April 21, 2010 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. Link to comment https://forums.phpfreaks.com/topic/199289-need-help-with-correct-syntax-for-if-condition/ Share on other sites More sharing options...
taquitosensei Posted April 21, 2010 Share Posted April 21, 2010 this should work $bigstring = "automobile"; $littlestring = "mobile"; if( !strpos($bigstring, $littlestring) ) { // Do something } you had an extra "(" Link to comment https://forums.phpfreaks.com/topic/199289-need-help-with-correct-syntax-for-if-condition/#findComment-1046005 Share on other sites More sharing options...
capella07 Posted April 21, 2010 Author Share Posted April 21, 2010 Great! Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/199289-need-help-with-correct-syntax-for-if-condition/#findComment-1046006 Share on other sites More sharing options...
DavidAM Posted April 22, 2010 Share Posted April 22, 2010 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 Link to comment https://forums.phpfreaks.com/topic/199289-need-help-with-correct-syntax-for-if-condition/#findComment-1046177 Share on other sites More sharing options...
capella07 Posted April 22, 2010 Author Share Posted April 22, 2010 Thanks, David Actually, as soon as I read taquito's reply I remembered I had something similar on another page, which also had the "=== false" in it. Works like a charm! Link to comment https://forums.phpfreaks.com/topic/199289-need-help-with-correct-syntax-for-if-condition/#findComment-1046436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.