News for hackers. Stuff that matters.
Re:PHP WTF: A PHP Annoyance Posted Every Day by apgwoz on Friday June 19, 2009 @09:39AM (#665082)
Maybe I shouldn't, but I find it sort of maddening that, PHP WTF would be powered by software written in PHP. I mean, if it bothers this guy to the extreme that he would go register a domain, and put up a website about it, it shouldn't be much trouble to learn a brand spanking new programming language that doesn't have all the problems he's discussing, right?

PHP has many problems. We know this. There are millions of words online that will attest to this fact. The fact is, all these words are for naught if people don't step in and do something about it.

Re:PHP WTF: A PHP Annoyance Posted Every Day by idlewords on Friday June 19, 2009 @09:51AM (#665106)
People who make heavy use of a language are in a good position to vent about its shortcomings. Sometimes a language is the best tool for the job despite its many deficiencies, and I'd much rather read these kind of rants by someone who knows the language inside out. I hate PHP more than I hate my own mother, and yet I still chose it for my most recent project. Every programming language has shortcomings, it's just a question of picking the best shortcomings for the job.
Re:PHP WTF: A PHP Annoyance Posted Every Day by bdotdub on Friday June 19, 2009 @10:14AM (#665140)
Yeah, he even has some sort of disclaimer in his footer

    This site is mostly written by chx, one of the core developers of the PHP-based Drupal project. It's not meant to be a bashing of PHP or its authors.
Re:PHP WTF: A PHP Annoyance Posted Every Day by randallsquared on Friday June 19, 2009 @01:58PM (#665595)
I hate PHP more than I hate my own mother

I've heard of damning with faint praise, but this is more like faint condemnation...

Re:PHP WTF: A PHP Annoyance Posted Every Day by apgwoz on Friday June 19, 2009 @07:22PM (#665935)
> Every programming language has shortcomings.

Name a language with more. I'll wait.

Re:PHP WTF: A PHP Annoyance Posted Every Day by j_baker on Friday June 19, 2009 @10:35PM (#666064)
INTERCAL
Re:PHP WTF: A PHP Annoyance Posted Every Day by smokinn on Saturday June 20, 2009 @02:03AM (#666191)
RPG/LE
Re:PHP WTF: A PHP Annoyance Posted Every Day by j_baker on Saturday June 20, 2009 @12:50PM (#666550)
vbscript
Re:PHP WTF: A PHP Annoyance Posted Every Day by jjs on Friday June 19, 2009 @10:01AM (#665121)
By using PHP, he'll never run out of material. :)
Re:PHP WTF: A PHP Annoyance Posted Every Day by mechanical_fish on Friday June 19, 2009 @12:17PM (#665398)
You can move to a new language whenever you want. But will your community follow you? Not without a great deal of groundwork.
Re:PHP WTF: A PHP Annoyance Posted Every Day by edw519 on Friday June 19, 2009 @09:44AM (#665091)
Anyone squatting on PHPWhining.com?
Re:PHP WTF: A PHP Annoyance Posted Every Day by apgwoz on Friday June 19, 2009 @09:48AM (#665103)
As a matter of fact, it looks like it is currently available. feel free to register it and collect all your favorite links to websites complaining about PHP!
Re:PHP WTF: A PHP Annoyance Posted Every Day by niyazpk on Friday June 19, 2009 @10:08AM (#665133)
"Posted Every Day" ?

I could only find 2 posts in the website. Wake me up when it has at least a dozen articles.

Re:PHP WTF: A PHP Annoyance Posted Every Day by Pistos2 on Friday June 19, 2009 @10:19AM (#665151)
Agreed; it might have been better marketing to have at least 6 posts up before announcing the site.
Re:PHP WTF: A PHP Annoyance Posted Every Day by bemmu on Friday June 19, 2009 @11:40AM (#665305)
If you want the strict non-coerced equality, I think you are supposed to use === as in javascript.
Re:PHP WTF: A PHP Annoyance Posted Every Day by Deestan on Friday June 19, 2009 @10:08AM (#665132)
So:

  0 == 'x'
results in

  bool(true)
Does anyone know the technical reasons for this?
Re:PHP WTF: A PHP Annoyance Posted Every Day by rbanffy on Friday June 19, 2009 @10:19AM (#665153)
daeken explained it quite well. From his post:

"As explained under 'read more', it's because 'test' will be cast to a number for comparison. Since 'test' isn't a number, it becomes 0, which is equivalent."

Re:PHP WTF: A PHP Annoyance Posted Every Day by mlLK on Friday June 19, 2009 @12:55PM (#665473)
These tables illustrate how complicated type checking in PHP really is: http://www.php.net/manual/en/types.comparisons.php
Re:PHP WTF: A PHP Annoyance Posted Every Day by AlphaEvolve on Friday June 19, 2009 @11:57AM (#665355)
if ($countarticles == '2') { postponeLaunch($credibility); }
Re:PHP WTF: A PHP Annoyance Posted Every Day by Jem on Friday June 19, 2009 @01:55PM (#665587)
No need for the '' around 2 ;)
Re:PHP WTF: A PHP Annoyance Posted Every Day by LogicHoleFlaw on Friday June 19, 2009 @02:35PM (#665658)
It's PHP, it works anyway.
Re:PHP WTF: A PHP Annoyance Posted Every Day by apgwoz on Friday June 19, 2009 @07:23PM (#665937)
Maybe he'll pay you for writing his 3rd post.
Re:PHP WTF: A PHP Annoyance Posted Every Day by Jem on Friday June 19, 2009 @02:56PM (#665687)
I know, I was just being pedantic :p
Re:PHP WTF: A PHP Annoyance Posted Every Day by xtho on Friday June 19, 2009 @04:23PM (#665764)
If they ever make it to posting #3, there is a chance this site will outlive php.
Re:PHP WTF: A PHP Annoyance Posted Every Day by erikwiffin on Friday June 19, 2009 @10:29AM (#665168)
You need a submission form. or not... there's no way you could sort through all the submissions you'd get.

If you can keep it from devolving into thedailywtf.com then you'll have a very loyal reader in me. I work in PHP and I would love to hear a list of valid frustrations instead of the general PHP bashing.

Re:PHP WTF: A PHP Annoyance Posted Every Day by TallGuyShort on Friday June 19, 2009 @10:42AM (#665194)
Did anyone read this disclaimer at the bottom of the page?

"It's not meant to be a bashing of PHP or its authors."

And if so, does anyone understand how that site isn't mean to be a bashing of PHP? I mean, he's free to bash it if he wants, but does that even make sense?

Re:PHP WTF: A PHP Annoyance Posted Every Day by RossM on Friday June 19, 2009 @01:10PM (#665494)
Yes. While it seems to be the trend to bash on PHP there's nothing wrong with discussing it's shortcomings in a constructive manner.
Re:PHP WTF: A PHP Annoyance Posted Every Day by AlphaEvolve on Friday June 19, 2009 @11:42AM (#665316)
please someone create aspwtf
Re:PHP WTF: A PHP Annoyance Posted Every Day by j_baker on Friday June 19, 2009 @10:34PM (#666063)
While we're doing outdated technologies, can we also do a COBOL wtf?
Re:PHP WTF: A PHP Annoyance Posted Every Day by DougBTX on Friday June 19, 2009 @09:36AM (#665074)
OK, I'm stumped, why is this?

  echo "<?php if (0 == 'test') echo 'true???'; ?>" | php # => "true???"
Re:PHP WTF: A PHP Annoyance Posted Every Day by daeken on Friday June 19, 2009 @09:39AM (#665081)
As explained under 'read more', it's because 'test' will be cast to a number for comparison. Since 'test' isn't a number, it becomes 0, which is equivalent.
Re:PHP WTF: A PHP Annoyance Posted Every Day by Sharlin on Friday June 19, 2009 @11:01AM (#665227)
Which is, of course, a deeply broken way to handle the situation. If multi-type comparisons have to be supported at all, at least one ought to cast the narrower type to the wider one! In this case, the int argument ought to be cast to string, because all ints can be uniquely represented as a string, but not the other way around.

As it stands, PHP's equality operator is not an equivalence relation because it's not transitive. This is deeply unintuitive and logic-defying.

Re:PHP WTF: A PHP Annoyance Posted Every Day by ars on Friday June 19, 2009 @03:45PM (#665730)
== is not the equality operator. === is the equality operator.

== is the type coerce/cast and then compare operator.

I agree with you about the way strings get converted to int instead of the other way around. I believe this is a backwards compatibility issue, and if they could change it they would, but it's too late.

It's also because of 1 == "01" would be false otherwise. And if you think "01" is strange you forget that all input to a php program comes from web forms, and is always a string.

Re:PHP WTF: A PHP Annoyance Posted Every Day by catch23 on Friday June 19, 2009 @04:55PM (#665804)
in javascript:

1 == '01' --> true

0 == 'x' --> false

1 == '1' --> true

true == 'asdf' --> false

'-2' == 2 --> true

'-2' == 0 --> false

Seems like in javascript, the coercing is much more sane. In php, all of the above statements would end up true.

Re:PHP WTF: A PHP Annoyance Posted Every Day by ars on Friday June 19, 2009 @05:34PM (#665841)
Javascript is not totally sane:

  '\t\r\n' == 0 --> true
And not transitive:

  "false" == "0" --> false
  "0" == 0 --> true
  0 == false --> true
  false == "false" --> false
It's basically impossible to make a loosely typed equality perfect.

PHP is much more strict about the rules used when doing the comparison, javascript is more loose. It tries to guess.

When comparing a string to a number, if the string looks like a number it's converted to one. But if it doesn't, then instead the number get converted to a string.

This works well most of the time - but not always, since it can surprise you. PHP is more strict, so it doesn't work as often, but it fails more consistently, so it's easier to find the problem.

Another example:

  "1" == "01" --> false
  "01" == 1 --> true
  1 == "1" --> true
Re:PHP WTF: A PHP Annoyance Posted Every Day by BerislavLopac on Saturday June 20, 2009 @01:40AM (#666176)
The funny thing is that in Javascript:

12 == parseInt('012')

returns false. :)

Re:PHP WTF: A PHP Annoyance Posted Every Day by mhansen on Saturday June 20, 2009 @03:38AM (#666237)
A leading zero usually means 'this number is octal', so parseInt returns 10. Not very intuitive notation, however.
Re:PHP WTF: A PHP Annoyance Posted Every Day by encoderer on Friday June 19, 2009 @05:19PM (#665829)
I don't have a PHP runtime in front of me, but IIRC the first one on your list and the last 2 will evaluate false in php
Re:PHP WTF: A PHP Annoyance Posted Every Day by encoderer on Friday June 19, 2009 @02:53PM (#665683)
I really would find this "deeply unintuitive" if I decided to try PHP with only intellisense as a reference. But by their very definition a language's idioms are not obvious. And this isn't my first rodeo, so I'd know that. And I'd read a little--just a little--before i jumped in.

Concepts and algorithms are universal. Operators and keywords are not.

Luckily, this is such a common need when learning a language that this information is kept all together in a tidy tabular way.

Because without it, I'd probably whip up an index.php and write something like print 'Welcome, ' + user.firstName;, get frustrated by parse errors, then by runtime errors, then write some impassioned comment about how deeply unintuitive it is to require prefixed variables and how using a dot for concatenation instead of a plus sign or ampersand is simply logic defying.

And that same table that explains concatenation and scope resolution is also going to give me a simple treatment on the == and === operators in this weakly typed language.

PHP is not an elegant language and given the choice, I'd nearly always prefer to be writing something else. But I took my time to respond to this because there's enough wrong with PHP that we don't need to resort to hyperbole to bash it.

Re:PHP WTF: A PHP Annoyance Posted Every Day by prodigal_erik on Friday June 19, 2009 @03:50PM (#665733)
I mostly agree, except that I don't think any treatment explaining

  NULL != "0" == false == NULL == 0 == "false" == true
is going to be simple.
Re:PHP WTF: A PHP Annoyance Posted Every Day by encoderer on Friday June 19, 2009 @05:13PM (#665825)
Sure it is:

To avoid implicit type coercion, use the identity operators === and !==.

Of course, the code you posted doesn't parse without adding parens. And when you do, it becomes much more obvious what it's doing and why.

Re:PHP WTF: A PHP Annoyance Posted Every Day by prodigal_erik on Saturday June 20, 2009 @05:33PM (#666846)
Sorry, I meant that as a bunch of compares that are mutually inconsistent, not one big expression. And "don't use ==" isn't really a solution until you persuade the runtime and every major library author to stop using it.
Re:PHP WTF: A PHP Annoyance Posted Every Day by encoderer on Saturday June 20, 2009 @09:38PM (#667028)
I don't get your point. Nobody has said "don't use equality." Equality is the right choice nearly all the time.

I've worked on some very large web apps (mostly C#, Python and PHP) and I can't recall ever really being foiled by php's implicit casting.

Just out of genuine curiousity, can you share an example where this caused a problem that you had to debug?

And if php implicit casting did somehow cause you problems, surely it was fixed by changing it from equality to identity?

So in summary... this could potentially cause a bug, but probably not, and if it does, it would be a consistent bug, and it would be fixed by a single keystroke.

Why, again, are we talking about this?

Re:PHP WTF: A PHP Annoyance Posted Every Day by ars on Friday June 19, 2009 @06:01PM (#665868)
That isn't legal PHP.
Re:PHP WTF: A PHP Annoyance Posted Every Day by petercooper on Saturday June 20, 2009 @09:58AM (#666417)
Unlike many blogs, at least this one will easily have enough material to keep going for several years.. ;-)
Re:PHP WTF: A PHP Annoyance Posted Every Day by lsb on Friday June 19, 2009 @11:41AM (#665308)
That's like shooting fish in a barrel.
Re:PHP WTF: A PHP Annoyance Posted Every Day by quizbiz on Friday June 19, 2009 @12:39PM (#665437)
But how do you get the fish into a barrel?
Re:PHP WTF: A PHP Annoyance Posted Every Day by dugmartin on Friday June 19, 2009 @02:47PM (#665676)
You shoot them in the lake.