Live data from Hacker News

Coding Horror: The PHP Singularity

codinghorror.com

331–340 of 341 posts

Re: Coding Horror: The PHP Singularity

#331
post #57
post #19

PHP is broken in many little aspects but if you use a high quality framework, it's quite enjoyable to work with. The main gripes I have with it are in the details, like: You can't do: $something->doStuff()[0] if(empty($something->doStuff())) Overload fucking functions. Fuck. What sucks: The namespace separator. The naming conventions of the core functions. The speed. But, overall it works and elegant solutions can be…

Speed isn't an issue, at least in my experience, compared to similar languages. Raw PHP usually beats Rails handsdown, and most PHP frameworks are usually comparable. PHP performance has increased markedly over the past few years, and particularly in v5.4. And if you really need a speed bump you can use the APC cache or HipHop compiler. Its never going to be as snappy as, say, C, but in the class of languages its in…

I've had to implement a feature in C (it was a batch process analysing some data and spitting out ~250k rows) because it was incredibly slow in PHP. As others have said, Ruby sucks for speed too.

Re: Coding Horror: The PHP Singularity

#332

I wish bloggers would stop quoting that fractal article. At least 50% of what's written in there is totally wrong/false. Other information is terribly out of date. And even more information is merely half-truths and lack of understanding of the language. The article author clearly scanned through PHP bashing articles and took material from them verbatim; mistakes and all. I'm not going to argue that PHP is a great la…

Come on, the guy is right about a lot of things. But as he said, there are things about any language that he hates. What's the big deal? Defame php all you want, I won't defend it, I just use it!

Re: Coding Horror: The PHP Singularity

#333
post #327

Earlier quoted context omitted.

> It IS a good thing, but it stands out when most of the language is extremely weakly-typed You only need distinct operators if your language is weakly typed. If you language is strongly typed like Python, Java, or C# then there is never any conflict between addition and concatenation. VB/VB.NET is also weakly typed and has separate operators. > So some things (objects) are passed by reference implicitly, and some (a…

tl;dr: I value consistency in my tools because it's a great measure of how frequently they'll trip me up and get in my way while reading or writing code. My biggest problem with PHP above all else, which I tried to write my article around, is that it's an inconsistent jumble from top to bottom. You're absolutely correct, of course, in that you can write decent PHP by avoiding large chunks of the language, memorizing…

> I'm under the impression that Java is the same.

No, Java is not the same. Scalars not objects in Java and are passed by value. C# is similar, except scalars are value-type objects and are passed by value.

> I'm assuming you're using the explicit cast inside the hypothetical function.

That's not what we were talking about. It's incredibly hard to have a reasonable conversation about this with you because you clearly only know a very small set of similar languages and have no experience with PHP at all.

> You appeared to be citing interpreted-ness as a reason for having a wonky module system.

You cited byte-code interpreted as if that's somewhat significant. PHP's module system really isn't very "wonky" -- it works as advertised. But since you don't use it, you wouldn't know.

> It'll tell me what things I've already done; I can't easily find out what I should be avoiding proactively.

You could read the manual. If you're new to PHP, you'd probably code strictly from the start.

> So what? Both are values; why is there any distinction?

Most languages make the distinction. Why shouldn't there be? That's really ugly code.

> Hell, you can call methods on numbers in Ruby and Python.

That's fine. It doesn't mean every language should do the same. Most languages, especially those PHP is based on, do not do that. Maybe one day PHP will have that but because it's weakly typed, it's difficult to know even what methods should exist. In PHP, it's perfectly reasonable to say strlen(1). Does that mean every number should have string methods? I'm guessing you never considered that?

> The problem with mysql_escape_string was that it didn't take the current database handle into account.

Yes, because the encoding properties of the current database connection are needed for encoding properly. If have two database connections, then what? It picks the first connection as the default and you encode incorrectly? Or if you had a second connection, do you just break all the existing code? What if the second connection doesn't happen every time? Then you're code is broken only every second Wednesday?

You've got a very singular perspective on things. That's fine. But by critiquing another language without knowing it you're making a lot of assumptions and blowing things way out of proportion.

Re: Coding Horror: The PHP Singularity

#334
post #328

Earlier quoted context omitted.

The real point here is that you're not a PHP programmer. You're complaining about a language you don't use and trolled the web for this material without even testing much of it out yourself. And sadly your uninformed opinion is now the go-to article when talking about PHP flaws. You've won the Internet.

I drooled far more PHP into my terminal that weekend than you would believe. There were a scant few legitimate errors, and I've removed all the ones brought to my attention. The rest of it should be factually correct, in which case, why do you care that I listed it? If you're correct in that many of the things I listed aren't really a big deal, the reader will agree with you anyway, right? (Also, "trawled". No snark…

> If you're correct in that many of the things I listed aren't really a big deal, the reader will agree with you anyway, right?

Because you're supposedly educating non-PHP coders about PHP. Most PHP coders do, in fact, agree with me.

Re: Coding Horror: The PHP Singularity

#335

Earlier quoted context omitted.

You have obviously never worked on a million-line codebase. When you have crap like this in your language: > E.g., $i = 0; $a[$i] = $i++; makes a different array than $i = 0; $a[$i + 0] = $i++; you have no hope of making a truly large program work. There are just too many sources of itsy bitsy bugs; you'll never find them all. (I'm not a PHP user; I have no idea why those are different arrays. Don't bother explaining…

I don't use PHP much, but I was curious, so here's the answer you don't want: You get $a[1] = 0 the first way, and $a[0] = 0 the second way. PHP seems to evaluate the array index before the right hand side if there is something more than a $variable to evaluate; otherwise PHP evaluates the array index after the right hand side, after $i has been incremented. I haven't used hiphop, but I'm curious if it preserves that…

The entire point of my post was that we preserve the semantics of this (and many other) PHP-isms. We have to, because you never can tell which glitches useful code has come to depend on.

Re: Coding Horror: The PHP Singularity

#336
post #327

Earlier quoted context omitted.

tl;dr: I value consistency in my tools because it's a great measure of how frequently they'll trip me up and get in my way while reading or writing code. My biggest problem with PHP above all else, which I tried to write my article around, is that it's an inconsistent jumble from top to bottom. You're absolutely correct, of course, in that you can write decent PHP by avoiding large chunks of the language, memorizing…

> I'm under the impression that Java is the same. No, Java is not the same. Scalars not objects in Java and are passed by value. C# is similar, except scalars are value-type objects and are passed by value. > I'm assuming you're using the explicit cast inside the hypothetical function. That's not what we were talking about. It's incredibly hard to have a reasonable conversation about this with you because you clearly…

> No, Java is not the same. Scalars not objects in Java and are passed by value.

How are pass-by-value and pass-by-object distinguishable for immutable values?

> That's not what we were talking about.

Yes it is. You said (array) as a cast is useful because a function can accept either a single value or an array of values, and the (array) cast will produce an array either way. I contend that doing this doesn't really work, because passing a single value that happens to be an object will cause the function to cast it to an array of the object's contents, which will have fascinating but useless results.

> You cited byte-code interpreted as if that's somewhat significant.

I only mentioned bytecode for the sake of pedantry: strictly speaking Python isn't interpreted, whereas e.g. shell is.

> You could read the manual.

I had to do quite a lot of that. It's kind of wordy. :)

> Most languages make the distinction. Why shouldn't there be? That's really ugly code.

I'm not proposing that people write code like this. I'm objecting to the inconsistency between a value in a variable and a value in source code.

Note that, for the same reason, you also can't chain calls like `foo()()`. There ARE good reasons for that: you could return a function name, or a closure, or an object that supports being called. But because the parser doesn't just allow trying to use parentheses on an arbitrary value, this doesn't work. Just like `foo()[]` didn't work until recently, because it too had to be special-cased.

> That's fine. It doesn't mean every language should do the same. Most languages, especially those PHP is based on, do not do that.

Really? C doesn't have methods. C++ has methods, but string literals are C strings so they have no methods. Perl doesn't consider builtin types to be objects, but if you turn them into objects with `autobox`, methods work equally well on variables and literals. Python allows method calls on literals -- and will let you try to call them, but of course literal strings aren't callable so this won't do anything. It parses, though. Ruby is the same. "foo".equals("bar") is a common Java idiom.

What language are you thinking of where there's a distinction made -- in the parser, no less -- between a variable and an expression?

> Maybe one day PHP will have that but because it's weakly typed, it's difficult to know even what methods should exist. In PHP, it's perfectly reasonable to say strlen(1). Does that mean every number should have string methods? I'm guessing you never considered that?

Sure, why not? Not that I was suggesting PHP direly needs scalar methods, but if a number responds to strlen(), why not to ->strlen()?

> Yes, because the encoding properties of the current database connection are needed for encoding properly. If have two database connections, then what?

Then you're screwed no matter what Zend does and still need to fix your code. So the options were: 1. Leave the existing function totally broken. Create a new one with a confusing and even longer name. Make everyone fix their code and remember to use the new function in the future. 2. Fix the issue in-place for the vast majority of existing code. Make the edge cases fix their code by doing the same thing they have to do with every other API function anyway. Optionally, throw a warning when multiple handles exist and mysql_escape_string is called with only one argument.

What, then, is the appeal of (1)?

> You've got a very singular perspective on things. That's fine. But by critiquing another language without knowing it you're making a lot of assumptions and blowing things way out of proportion.

I'm hearing this a lot, but (as far as I can be trusted to say this) I don't see it. I draw a lot of comparisons to Python and Perl because they're in the same family and I know more about their inner workings than anything else. That doesn't mean they're the only languages I've used or the only languages I think are worthwhile.

Yes, I picked on a lot of little things. It's the little things that matter. Every language (ahem, most) has function calls, data structures, and loops. What differentiates them is the little day-to-day stuff.

So what am I missing? What makes these design decisions good, what makes PHP great? I've heard a lot of defenses and a lot of statistics, but not too many outright advantages. Most of your original response is just workarounds, not reasons why the behavior is good or even acceptable. The image I'm left with is that of an overgrown playground filled with active mines, but most of the mines have upturned buckets over them so you probably won't step on them, and that's supposed to make it a great place to send your kids. If even PHP's supporters can do no better than explain how to write passable PHP when I have a gun to my head, why would I want to use it?

Re: Coding Horror: The PHP Singularity

#337
post #328

Earlier quoted context omitted.

I drooled far more PHP into my terminal that weekend than you would believe. There were a scant few legitimate errors, and I've removed all the ones brought to my attention. The rest of it should be factually correct, in which case, why do you care that I listed it? If you're correct in that many of the things I listed aren't really a big deal, the reader will agree with you anyway, right? (Also, "trawled". No snark…

> If you're correct in that many of the things I listed aren't really a big deal, the reader will agree with you anyway, right? Because you're supposedly educating non-PHP coders about PHP. Most PHP coders do, in fact, agree with me.

The arguments I've read supporting PHP scare me a little. They seem to boil down to a few things:

* It's not so bad, really. Here's a map of land mines to avoid. Hopefully it's complete!

* PHP is everywhere. Apparently COBOL and Visual Basic were also excellent languages.

* PHP is easy to start with. Newbies don't want to learn anything too hard; they just want to throw down some code. What could possibly go wrong?

* PHP is easy to deploy, as if that's an inherent property of PHP and not something that can be fixed for other languages.

I'd love to hear a real argument for why PHP is a great language for Web development. Not just a defense of PHP. We get it: every language has quirks. I code in C++, willingly even. I know a thing or two about whacky language quirks outside of PHP. But why should I choose PHP over all of my other options?

What makes PHP so great?

Re: Coding Horror: The PHP Singularity

#338

Earlier quoted context omitted.

> If you're correct in that many of the things I listed aren't really a big deal, the reader will agree with you anyway, right? Because you're supposedly educating non-PHP coders about PHP. Most PHP coders do, in fact, agree with me.

The arguments I've read supporting PHP scare me a little. They seem to boil down to a few things: * It's not so bad, really. Here's a map of land mines to avoid. Hopefully it's complete! * PHP is everywhere. Apparently COBOL and Visual Basic were also excellent languages. * PHP is easy to start with. Newbies don't want to learn anything too hard; they just want to throw down some code. What could possibly go wrong? *…

> PHP is easy to deploy, as if that's an inherent property of PHP and not something that can be fixed for other languages.

Other languages can fix that (they haven't yet) but at the same time PHP is fixing it's problems at a rapid pace. It seems that PHP is making more progress fixing it's problems and including modern features than other languages are improving their deployment situation.

One advantage of PHP, in my opinion, is that it supports multiple levels of commitment. I recently created a static website for my business; I didn't want the hassle of using a CMS or Wordpress. But I used PHP to include the common headers and footers and handle my contact form. In any other web platform (including PHP with a framework) is terrible overkill for a job like that. But if I want to do something more significant in the future, I can.

PHP's work cycle is also great; if done right you should have no build step. Just edit and hit refresh. I'm currently doing ASP.NET and it's just not possible to iterate as fast during development.

But honestly, I've used a lot of different platforms and PHP just isn't that different. Why choose PHP over all your other options? If your goal is distribution of your app then you can't go wrong with PHP because it's installed everywhere. And, depending on your language experience, you might be more comfortable in PHP because it's very literal; there's no monkey patching or heavy use of dynamic features. But really, there's no reason not use Ruby or Python over PHP.

Re: Coding Horror: The PHP Singularity

#339
post #336

Earlier quoted context omitted.

> I'm under the impression that Java is the same. No, Java is not the same. Scalars not objects in Java and are passed by value. C# is similar, except scalars are value-type objects and are passed by value. > I'm assuming you're using the explicit cast inside the hypothetical function. That's not what we were talking about. It's incredibly hard to have a reasonable conversation about this with you because you clearly…

> No, Java is not the same. Scalars not objects in Java and are passed by value. How are pass-by-value and pass-by-object distinguishable for immutable values? > That's not what we were talking about. Yes it is. You said (array) as a cast is useful because a function can accept either a single value or an array of values, and the (array) cast will produce an array either way. I contend that doing this doesn't really…

> How are pass-by-value and pass-by-object distinguishable for immutable values?

They aren't really. There's no real technical difference, just a terminology difference. All that you need to know is that whether it be Python, PHP, Java, or whatever that passing around scalars and objects works out the same.

> You said (array) as a cast is useful because a function can accept either a single value or an array of values, and the (array) cast will produce an array either way.

I never said anything about functions -- just that the cast itself is useful.

> because passing a single value that happens to be an object will cause the function to cast it to an array of the object's contents, which will have fascinating but useless results.

People cast objects to arrays in PHP all the time because that can be a useful operation. I understand you point, but I don't really think it's too valid. If you're explicitly doing a cast, you should know what you're casting from. This is sound advice no matter what language you're using.

> strictly speaking Python isn't interpreted, whereas e.g. shell is.

It's an implementation detail that's not significant. The next version of bash should byte-code compile everything behind the scenes and run it (like PHP does) and it wouldn't make any difference. You could write an interpreter for C code (they exist) and it wouldn't change anything about C.

> Note that, for the same reason, you also can't chain calls like `foo()()`.

Honestly, there's no language grammar reason why that shouldn't work in PHP. It's just that parser sucks and they're very slow/careful to make changes to the parser. If it's a function name, a closure, or an object that supports being called they're called the same. If you can do $foo() then you should be able to do foo()(). The same is true about foo()[]. These things don't have to special-cased so much as the parser has to be modified to be more generally accepting.

> What language are you thinking of where there's a distinction made -- in the parser, no less -- between a variable and an expression?

PHP makes no distinction between a variable and an expression (except as described above). It's make a distinction between scalars and objects.

> Sure, why not? Not that I was suggesting PHP direly needs scalar methods, but if a number responds to strlen(), why not to ->strlen()?

Then there's really no point in having scalars be objects. You could add syntactic sugar to make strlen($x) and $x->strlen() do exactly the same thing but what would be the point? I'd rather they do something more interesting with that ability. I think it's the only way they can successfully implement unicode.

> Leave the existing function totally broken. Create a new one with a confusing and even longer name. Make everyone fix their code and remember to use the new function in the future.

MySQL made this decision, not PHP. They just give you the library to use. Anyway, you seem to have a problem with the concept of backwards compatibility -- it generally does mean "continue to use this broken thing because you don't want to change your code".

> I'm hearing this a lot, but (as far as I can be trusted to say this) I don't see it.

Someone tells you that you lack perspective and you don't see it -- so ironic! (I'm just being cheeky)

> I draw a lot of comparisons to Python and Perl because they're in the same family and I know more about their inner workings than anything else. That doesn't mean they're the only languages I've used or the only languages I think are worthwhile.

Yes, but going off on public/protected/private as if it's a flaw in PHP because you don't like them is not a valid critique. That's just one example but there are others. Even saying everything should be an object is more personal taste than anything else. I very much like "everything is an object" but it's not clear how best to implement that in PHP yet.

> It's the little things that matter. Every language (ahem, most) has function calls, data structures, and loops. What differentiates them is the little day-to-day stuff.

I disagree. I think I probably switch between languages a bit more than you do and the little things don't matter that much. What matters is the big things. Most of the stuff you mention in your article, if I encounter them at all, I don't even notice. It's such a non-issue that unless you bring it up, I wouldn't think to tell you. In fact, in some cases, I wasn't even sure what you were talking about because I don't encounter them (the lack of stack traces on errors, for example).

> I've heard a lot of defenses and a lot of statistics, but not too many outright advantages.

Indeed. I'm not saying PHP has advantages over other platforms -- it has some -- but, in the end, the differences between any of the platforms aren't significant and mostly personal taste. I continue to code in PHP because I know PHP, I know the frameworks, and I have lots of existing PHP code. If I had to write Python or Ruby, I would. I'm going to be writing ASP.NET code for next 6 months.

I just think your article blows everything out of proportion. Much of it is "look how this crazy unreasonable code reacts in PHP". It's not invalid but it's not really indicative of developing in PHP. Similar lists have been made for JavaScript and yet that doesn't affect opinions too much. Similar lists have been made for C and C++ but everybody knows what they're getting into there.

Here we are talking about mysql_real_escape_string() and that's as legacy as you can get.

Re: Coding Horror: The PHP Singularity

#340
post #166

Earlier quoted context omitted.

Why is python or ruby too hard for your dad to learn? If anything I would think something like ruby would be even easier than php since things are a bit more consistent. I just don't see how PHP is more approachable, especially compared to the supposed ease of ruby on rails.

He's been an HTML coder for about 10 years doing sites with front page and other html authoring tools. Him figuring out is fairly easy when he already understands html.

That makes sense then. There is more of a shift in thinking going from html to python vs php.
Post reply on HN