RES: RES: string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Never late to learn new stuff, you're right Stut.

Thanks! 

-----Mensagem original-----
De: Stut [mailto:stuttle@xxxxxxxxx] 
Enviada em: segunda-feira, 7 de abril de 2008 10:42
Para: Thiago Pojda
Cc: 'John Taylor-Johnston'; 'PHP-General'
Assunto: Re: RES:  string

Thiago Pojda wrote:
> <?php
> $name = "John Taylor";
> 	if (strpos($name,'John') > 0){ 
> 	//you could use stripos for case insensitive search
> 		echo "found";
> 	}
> ?>

This will not do what you expect it to. Since 'John' is the 
first thing in the string strpos will return 0 causing the 
condition to evaluate to false.

As per the documentation for strpos you should compare the value *and
type* of the variable returned by strpos against false to check 
for non-existance.

if (strpos($name, 'John') !== false) { ... }

-Stut

--
http://stut.net/

> -----Mensagem original-----
> De: John Taylor-Johnston [mailto:jt.johnston@xxxxxxxxxxxxxx]
> Enviada em: segunda-feira, 7 de abril de 2008 10:25
> Para: PHP-General
> Assunto:  string
> 
> $name = "John Taylor";
> I want to verify if $name contains "john", if yes echo "found"; Cannot 
> remember which to use:
> http://ca.php.net/manual/en/ref.strings.php
> Sorry,
> John
> 
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe,
> visit: http://www.php.net/unsub.php
> 
> 
> 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux