Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old June 29th, 2006, 03:15 PM
drec
Guest
 
Posts: n/a
Default strpos question

I am trying to right an if statment using strpos to determine if the
string exists in the variable, however I seem to be getting the wrong
effect. Here is my script.

<?php
$dn = "ABC-DEF";

$pos = strpos( $dn, "ABC-DEF" );
if ( $pos != FALSE )
{
echo 'variable contains value';
}
else
{
echo ' variable does not contain value' ;
}
?>

It keeps returning does not contain value. I am kinda new to strpos,
any suggestions?

  #2  
Old June 29th, 2006, 04:15 PM
Erwin Moller
Guest
 
Posts: n/a
Default Re: strpos question

drec wrote:
[color=blue]
> I am trying to right an if statment using strpos to determine if the
> string exists in the variable, however I seem to be getting the wrong
> effect. Here is my script.
>
> <?php
> $dn = "ABC-DEF";
>
> $pos = strpos( $dn, "ABC-DEF" );
> if ( $pos != FALSE )
> {
> echo 'variable contains value';
> }
> else
> {
> echo ' variable does not contain value' ;
> }
> ?>
>
> It keeps returning does not contain value. I am kinda new to strpos,
> any suggestions?[/color]

Yes, read the manual. :-)

It is such a common mistake php.net shouts a warning when describing the
function.

Read this:
http://nl2.php.net/manual/en/function.strpos.php

Quote:
Warning

This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function.
I think yours is a typical 0 that evaluates to false.


Regards,
Erwin Moller

  #3  
Old June 29th, 2006, 04:25 PM
Kimmo Laine
Guest
 
Posts: n/a
Default Re: strpos question

drec kirjoitti:[color=blue]
> I am trying to right an if statment using strpos to determine if the
> string exists in the variable, however I seem to be getting the wrong
> effect. Here is my script.
>
> <?php
> $dn = "ABC-DEF";
>
> $pos = strpos( $dn, "ABC-DEF" );
> if ( $pos != FALSE )
> {
> echo 'variable contains value';
> }
> else
> {
> echo ' variable does not contain value' ;
> }
> ?>
>
> It keeps returning does not contain value. I am kinda new to strpos,
> any suggestions?[/color]

RTFM: http://php.net/strpos

Strpos returns the position of $haystack where $needle begins, otherwise
false if $needle is not found. In this case, the position is zero. When
you compare integer value zero with boolean value false in loose typed
language, they match. If you want to strictly compare and distinguish
the difference between boolean false and integer zero, you need to use
strict comparison operators.

Use:
if ( $pos !== FALSE )

!== is the type specific strict comparison operator, which checks also
for variable types as well as values. Then, when you compare boolean
false to integer zero, they do not match, but false and false will.

This is all explained in the manual if you'd just care to Read The Fine
Manual.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.