RE: [PHP] strpos mystery

2004-07-29 Thread Ford, Mike [LSS]
On 29 July 2004 01:50, Jon Drukman wrote:

> with this code fragment:
> 
>  
> $string='/mobile/phone.html';
> if (strpos($string,'/mobile/')!==false) { print "one: yes\n"; }
> if (strpos($string,'/mobile/')===true) { print "two: yes\n"; }
> 
> > 
> 
> 
> only the first if statement prints anything.  why is !==
> false not the
> same as === true ?

Because strpos returns the integer offset of the found substring, or FALSE
if not found; it *never* returns TRUE.  (You need the !== test because
strpos() can return an offset of zero, which would be ==FALSE but not
===FALSE.)

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] strpos mystery

2004-07-28 Thread Jason Barnett
Heck, even I got it wrong ;)  True check below should always fail...
Jason Barnett wrote:
Because === and !== check the type as well. Of you set $string =
'blah' you'll still get the same result.
If you were using != and == both would print.
strpos() returns an int, so comparing it to false with === is always
false. The same would be true for true.

That's half right.  strpos actually *can* return false as opposed to 0, 
so checking doing the === check might be necessary for your application.


$string = 'blah';
if (strpos($string, 'not in the original $string') === false){
  echo 'False check succeeded - not in $string.';
}
if (strpos($string, $string) === true) {
  echo 'True check succeeded - in string';
} else {
  echo 'True check failed - because strpos was at offset 0.';
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] strpos mystery

2004-07-28 Thread Jason Barnett
Because === and !== check the type as well. Of you set $string =
'blah' you'll still get the same result.
If you were using != and == both would print.
strpos() returns an int, so comparing it to false with === is always
false. The same would be true for true.

That's half right.  strpos actually *can* return false as opposed to 0, so 
checking doing the === check might be necessary for your application.


$string = 'blah';
if (strpos($string, 'not in the original $string') === false){
  echo 'False check succeeded - not in $string.';
}
if (strpos($string, $string) === true) {
  echo 'True check succeeded - in string';
} else {
  echo 'True check failed - because strpos was at offset 0.';
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] strpos mystery

2004-07-28 Thread Justin Patrin
On Wed, 28 Jul 2004 17:50:01 -0700, Jon Drukman <[EMAIL PROTECTED]> wrote:
> with this code fragment:
> 
>  
> $string='/mobile/phone.html';
> if (strpos($string,'/mobile/')!==false) { print "one: yes\n"; }
> if (strpos($string,'/mobile/')===true) { print "two: yes\n"; }
> 
> ?>
> 
> only the first if statement prints anything.  why is !== false not the
> same as === true ?

Because === and !== check the type as well. Of you set $string =
'blah' you'll still get the same result.

If you were using != and == both would print.

strpos() returns an int, so comparing it to false with === is always
false. The same would be true for true.

> 
> -jsd-
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> !DSPAM:410846c1241919501214933!
> 
> 


-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php