Can't undestand...
Moderator: General Moderators
-
joaomrpereira
- Forum Newbie
- Posts: 6
- Joined: Fri Sep 01, 2006 4:45 pm
Can't undestand...
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
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
-
joaomrpereira
- Forum Newbie
- Posts: 6
- Joined: Fri Sep 01, 2006 4:45 pm
Re: Can't undestand...
Code: Select all
$startPos = strpos($strTest,$strNeedle_one);
if ( $startPos >== 0 ){}Code: Select all
$strTest = "this is a string";
$strNeedle_one = "string";
$startPos = strpos($strTest,$strNeedle_one);
if ( $startPos === true ) // allways evaluate [b]false[/b]!!!!-
joaomrpereira
- Forum Newbie
- Posts: 6
- Joined: Fri Sep 01, 2006 4:45 pm
-
joaomrpereira
- Forum Newbie
- Posts: 6
- Joined: Fri Sep 01, 2006 4:45 pm
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
http://fr3.php.net/manual/en/function.strpos.php
-
joaomrpereira
- Forum Newbie
- Posts: 6
- Joined: Fri Sep 01, 2006 4:45 pm
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
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