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

Link to comment
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.

Link to comment
Share on other sites

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

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.