Re: comparing a string

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

 



(inline)

Adam Zey wrote:
Rafael wrote:
A single "=" it's an assignment, not a comparison; and though it sometimes work, you shouldn't compare strings with "==", but using string functions, such as strcmp()... or similar_text(), etc.

This is PHP, not C. Operators such as == support strings for a reason, people should use them as such.

You shouldn't rely on what other languages do, but the one you're working with; PHP has no explicit data-types and manages strings as PERL does. Just as you say there's a reason why "==" supports strings, there's also a reason (or more) for strcmp() to exists --it's just safer to use strcmp() instead of "==", e.g: 24 == "24/7"

If you need to ensure type, (so that 0 == "foo" doesn't return true), then you can use ===.

Using a function call that does more than you need when there is an operator to achieve the same goal is bad advice.

I think you haven't encounter a "special case" to make you understand "==" does NOT have the same behaviour as strcmp() It's just like the (stranger) case of the loop
  for ( $c = 'a';  $c <= 'z';  $c ++ )
      echo  $c;

Not to mention the fact that it leads to harder to read code. Which of these has a more readily apparent meaning?

if ( strcmp($foo,$bar) == 0 )

if ( $foo === $bar )

That might be true, either way you need to know the language to understand what the first line does and that the second isn't a typo.
--
Atentamente / Sincerely,
J. Rafael Salazar Magaña

--
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