Jump to content

Strpos uses


dotkpay

Recommended Posts

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";
}

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.