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
https://forums.phpfreaks.com/topic/257569-strpos-uses/
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
https://forums.phpfreaks.com/topic/257569-strpos-uses/#findComment-1320204
Share on other sites

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.