dotkpay Posted February 22, 2012 Share Posted February 22, 2012 Hello, Am a little puzzled on how to effectively determine if a character exists in a string. I am trying to come to grips with the function strpos but its a little confusing. If am searching for an underscore in a string and want to echo yes if the string contains an underscore, would the code below be right? Would it be right to exclude booleans TRUE and FALSE Example 1 <?php $string = "Hello_World"; if(strpos($string, "_")) { echo "yes"; } OR do I have to include the booleans TRUE/FALSE Example 2 <?php $string = "Hello_World"; if(strpos($string, "_") == TRUE) { echo "yes"; } Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 Yes. Quote Link to comment Share on other sites More sharing options...
dotkpay Posted February 22, 2012 Author Share Posted February 22, 2012 You said yes, but to which example, the first or second? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Because strpos returns either an integer indicating the position in which the string was found (which may include 0), or FALSE if not found, use a strict comparison to NOT FALSE if you just want to see if the string is found. <?php $string = "Hello_World"; if( strpos($string, "_") !== FALSE ) { echo "yes"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.