You are here: Re: strpos question « PHP Programming Language « IT news, forums, messages
Re: strpos question

Posted by Kimmo Laine on 12/18/85 11:51

drec kirjoitti:
> 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?

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)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация