Can't undestand...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joaomrpereira
Forum Newbie
Posts: 6
Joined: Fri Sep 01, 2006 4:45 pm

Can't undestand...

Post by joaomrpereira »

Hi all,

First of all, let me say that I'm very newbie in php.

Analyse this, please:


$strTest = "this is a string";

$strNeedle_one = "not";


$startPos = strpos($strTest,$strNeedle_one);

if ( $startPos >= 0 ) // allways evaluate true!!!!

This is the thing I cannot understand. If the needle is not found in the string, why this allways evaluate true?

Sorry if this is a stupid question, but I cannot undesrand it.

Thanks,

JP
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because false is always greater than or equal to zero.
joaomrpereira
Forum Newbie
Posts: 6
Joined: Fri Sep 01, 2006 4:45 pm

Post by joaomrpereira »

Ok, clarified.

But... still confused with the following


$strTest = "this is a string";

$strNeedle_one = "string";


$startPos = strpos($strTest,$strNeedle_one);

if ( $startPos === true ) // allways evaluate false!!!!

thanks,
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Can't undestand...

Post by Luke »

Code: Select all

$startPos = strpos($strTest,$strNeedle_one);

if ( $startPos >== 0 ){}
Should give you the results you are expecting
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

$strTest = "this is a string";

$strNeedle_one = "string";


$startPos = strpos($strTest,$strNeedle_one);

if ( $startPos ===  true ) // allways evaluate [b]false[/b]!!!!
=== is the identical operator. true or false is not identical to any number in any case, so that will always evaluate to false since strpos returns anywhere from -1 on up.
joaomrpereira
Forum Newbie
Posts: 6
Joined: Fri Sep 01, 2006 4:45 pm

Post by joaomrpereira »

Thanks for the help, but :

Parse error: syntax error, unexpected '=' in ...\content.html.php on line 517


with if ( $startPos >== 0 ) {

:( :(
joaomrpereira
Forum Newbie
Posts: 6
Joined: Fri Sep 01, 2006 4:45 pm

Post by joaomrpereira »

The Ninja Space Goat wrote:
=== is the identical operator. true or false is not identical to any number in any case, so that will always evaluate to false since strpos returns anywhere from -1 on up.

My strpos does not return -1 when the needle is not found in the string :(
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm sorry I was thinking of a javscript function I think. It does return false if it can't find it:
http://fr3.php.net/manual/en/function.strpos.php
joaomrpereira
Forum Newbie
Posts: 6
Joined: Fri Sep 01, 2006 4:45 pm

Post by joaomrpereira »

confusing this one...

if I use:

if ( $startPos === true)

it allways return true, wheter the needle is found or not but with:

if ( $startPos !== false)

the behavior is what I expect....


strange
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strpos() does not return the boolean value true, ever. Using the identity operator to match against true will always be false. As the documentation says, use "!== false" to see if strpos() found something.

false, "", '', 0, '0' and null are all the same when using the equality based operators: >, <, >=, <=, ==, and !=.

The identity based operators check that the types match and the values match too.

Some further reading: http://php.net/language.operators.comparison
Post Reply