News for hackers. Stuff that matters.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by nuclear_eclipse on Monday August 25, 2008 @07:52PM (#286591)
So, it's just a colorized, otherwise identical rip off of what PHP already gives you as part of their official documentation? http://www.php.net/manual/en/types.comparisons.php

Blogspam is the bane of the internet...

Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by Harkins on Monday August 25, 2008 @07:59PM (#286597)
It also lacks the bolding and heavy cell borders and gutters that turn the PHP manual's table into a wall of gray noise. It's a solid improvement.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by tesseract on Monday August 25, 2008 @09:03PM (#286628)
This wording...

> The PHP Documentation Group has now added the Appendix K. PHP type comparison tables to the official PHP manual.

...seems to make the claim that the linked article came before the PHP manual's version. I'm too lazy to verify (e.g. by checking archive.org or something) though.

Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by time_management on Monday August 25, 2008 @08:12PM (#286607)
I'll admit that I know nothing about PHP, but please tell me it is a mistake in the design table that they have "foo" == 0. Otherwise, we have this rather disturbing* non-transitivity of (==):

true == "foo" == 0 == false

I also generally dislike the idea of having certain values being treated as "false", e.g. 0 is false but 2.2e-16 is true... or "a" and "1" are true but "0" is false. It just seems to be clumsy, and not very robust. It reminds me of the "and-or trick" in Python, which busts if you aren't familiar with the fact that values such as 0/0.0 are false.

* Actually, on review, it's obvious that some non-transitivity is desirable by design, in the sense that 1 and 2 are both "true" but we don't want 1 == 2, even though 1 == true and 2== true. However, I still maintain that the chain I have above represents bad design.

Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by wvenable on Monday August 25, 2008 @09:01PM (#286626)
The fact is, if you look at it in this abstract way then your feelings are valid. But during concrete web development, the functionality of PHP's comparison operators make a lot of sense and make the code simpler and easier to understand.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by time_management on Monday August 25, 2008 @09:21PM (#286646)
Why is this? I'm not sure I understand where the gain is.

I guess that, for me, the cost of a few keystrokes involved in writing "if x == 0 then ..." vs. "if x then ..." never seemed to be worth the hassle of remembering false values. I've always found it preferable to make the comparison being made explicit.

I also generally dislike the practice of using reserved values to signify empty data, e.g. 0 for missing numerical data. I prefer to use ML-style option types, e.g. None vs. Some 0, and in a dynamically typed language, I'd just use the nil/().

Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by wvenable on Monday August 25, 2008 @09:31PM (#286655)
On the web, all the data comes in as strings including numbers. So the comparisons work well the numbers in strings. The rest of the weirdness comes from PHP copying C and Perl semantics.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by cosmo7 on Tuesday August 26, 2008 @12:14AM (#286755)
Don't blame C. This is usually the point that programmers realize why statically typed languages are such a good idea.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by time_management on Tuesday August 26, 2008 @09:39AM (#287049)
When I moved from Java to Python, I was sure that dynamic typing was the way to go, because the type system in the C/Java family is such a hindrance, while lacking the power of a mature type system as seen in ML or Haskell. When I moved from Python to ML, I was equally adamant about static typing being the way to go, because the ML type system is pretty awesome in terms of compile-time error-checking and documentation. Now that I'm learning Lisp, I'm open to the benefits of dynamic typing, or at least want to keep an open mind about the matter.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by time_management on Monday August 25, 2008 @09:49PM (#286665)
This makes sense. I guess a programmer who does a string => bool type conversion desires different behavior than one who does string => int => bool, so the fact that they produce different results ("foo" => true vs. "foo" => 0 => false) is part of the design.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by apexauk on Monday August 25, 2008 @09:18PM (#286645)
in PHP "foo" == 0 due to how php handles type juggling - the string is converted to an integer before the comparison, with strings that don't contain numbers converted to 0. so "foo" == 0 and "0" == 0 but "1" != 0.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by time_management on Monday August 25, 2008 @09:24PM (#286647)
This makes more sense, but I still find it odd and disagreeable. I tend to prefer that my programs fail loudly, so I'd write string -> int conversion to raise an exception on seemingly non-numerical input like "foo", "ba6r.5", "pwn3d", etc. Then again, admittedly I know very little about web development. Is it not practical to do this?
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by Zak on Monday August 25, 2008 @10:41PM (#286699)
There is nothing special about web development that makes implicit typecasting or returning an arbitrary value when a cast fails instead of raising an exception a good idea. Another reasonable option is to return NaN (Javascript does this) or null.
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by apexauk on Tuesday August 26, 2008 @06:38AM (#286944)
agree about failing loudly when bad stuff happens - but exceptions are best reserved for truly exceptional circumstances. in webdev everything arrives from the browser as a string so implicit casting is more of the norm. if your user enters something dumb into the quantity field of your shopping cart it doesn't really warrant abandoning the rest of their input and leaping back up the call stack to a "misson failed"-level error handler.

functions like is_numeric('string') exist for the rarer situations where things like this actually matter, but php's default way is simply reflective of it's nature as a loosely-typed language.

Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by gtani on Tuesday August 26, 2008 @10:16AM (#287078)
i believe these casts, or whatever the implicit coercions are called, follow the perl rules, and along with rules for truth and falsehood, behave predictably. The chart is kind of overkill
Re:PHP cheat: ==, ===, empty(),is_null(),isSet() by tptacek on Monday August 25, 2008 @07:44PM (#286582)
This is PHP? Sold!