Jump to content

STRPOS not working? why?


RyuMaster

Recommended Posts

Hi! I have very simple comparing code.

The $str includes $compare, and should return "YES", but I get "NO". Why is it so?

Am I missing something?

 

<?php

$str = "According to the management guru Peter Drucker (1909–2005), the basic task of a .... Branches of management theory also exist relating to nonprofits and to";

$compare = "According to the management guru Peter";

if(strpos($str,$compare))

{

echo "YES";

}

else

{

echo "NO";

}

?>

 

Regards!

Konstantin.

Link to comment
https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/
Share on other sites

Yup since the match is right at the start (the matching starts at 0) and false equates loosely to 0 they create a false instead of a true. use

!== false

as suggested above or

=== true

 

I dont think === true will work since the function returns the integer position.

Correct, === true will not work. Just use !== false or === false.

Typo? I don't think so. Maybe I didn't explain what I meant well enough.

 

I mean like this:

<?php
$str = 'Blah Blah Blah. Hello World';
if(strpos($str, 'Blah') !== false) // Use !== false to see if it's present
{
echo 'Found';
}

if(strpos($str, 'Not found') === false) // Use === false to see if it's not present
{
echo 'Not Found';
}

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.