Re: Is this a bug ??
| From: | David Newcomb | Date: | Thu, 31 Aug 2000 14:03:33 +0000 |
| Subject: | Re: Is this a bug ?? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-14608@lists.php.net to get a copy of this message | ||
> ti980246@binus.ac.id wrote:
>
> > hellow.. is 0 equal with null ??
> > i've written a code using strpos to get position of substring in a
string.. here
> > is my snippet..
> > $besar="Sam";
> > $posisi=strpos($besar,"S");
> > if ($posisi=="") {print("NULL");}
> > if ($posisi==0) {print("first position");}
>
> > it runs the both if (it prints "NULL and "first position")
> > the second if means that the substring is on first position in a
string... but it
> > is a null too.. is this a bug ?? or i've misunderstanding the php's
variable
> > concept ?
>
> > thank's
>
> As the manual states in the documentation of strpos(), you should use
operator
> ===
> (is equal and of same type) to make difference between the first position
and
> the
> FALSE/null value.
>
> Thus (for PHP 4):
>
> <?php
> $besar = "Sam";
> $posisi = strpos( $besar, "S" );
> if (!isset($posisi) || $posisi === FALSE) {
> print "Not found.";
> } else {
> print "'S' found at $besar in position $posisi:
> '$besar[$posisi]'\n";
> }
> ?>
Just to add, strpos() always returns something so the isset() will always be
true. Therefore there is no need for it eg
if ($posisi === FALSE)
print "Not found";
else
print "founded at $posisi"
Well. It saves you 1 op!
David.