Ask HN: PHP for Python programmers
I've been happily programming in Python for a long time (6+ years) and so far I've been fortunate to never have to write a single line of PHP. Recently I was contacted by BigNameCorp regarding a good job opportunity. Problem is, PHP is the main language (among several others) of said company and chances are that at least part of the job will be writing and reading PHP code. Although that's not necessarily a show stopper, I'm afraid I have been spoiled working with a high level language for so long and I'm not particularly thrilled to move down the language totem pole.
I've seen blog posts about moving away from PHP to Python, Ruby, and other better languages but not much about the opposite direction. Have you been in a similar situation and if so how did you manage to adapt and maintain your sanity ? Any suggestions, warnings or pointers on learning the good parts of PHP (if there are any) for advanced programmers would be most helpful.
76 comments
[ 3.2 ms ] story [ 125 ms ] threadIt's true that everything gets shoveled into one big namespace, but PHP lets you get a lot done with very little bull. There's a lot of crappy PHP out there, but there's a lot of crappy Java, Python, Ruby and C# too. If you approach PHP with some discipline I think you'll find it's an environment that never kills you with a showstopper flaw.
But it's been absolutely bullet proof in production for me and when I did find a bug I've had a patch within 24 hours straight from the core devs.
They do take quality serious there, they just don't define it as language elegance.
PHP tends to be at the bottom of the "language quality totem pole" because as a language it's pretty low quality. The PHP interpreter may be fine as a piece of software, but that's a completely separate matter from being a quality language.
> PHP tends to be at the bottom of the "language quality totem pole" because as a language it's pretty low quality. The PHP interpreter may be fine as a piece of software, but that's a completely separate matter from being a quality language.
It is specifically for that reason that I broke it down in to two parts, and apparently we agree on the language quality from a design point of view, but from a 'have to use this in production' point of view we do not.
> You say the PHP devs have a sharper focus on quality than other languages;
That's a straw-man, I said no such thing, but it does happen to be my experience that the PHP devs respond very quickly compared to other platforms / languages.
> Can you provide and kind of evidence for those claims?
And no, I'm not going to provide you with any 'evidence' for my claims, you can take my word for it. This is not a court of law, and besides that I never made any claims about other languages before this comment and I don't feel like putting other languages and/or their developers down.
I know PHP as a language has its flaws and that there are some unfortunate choices made in the past that are very hard to undo but that's no reason for losing all objectivity.
It's great that the PHP devs do that! Is that a compelling reason to use the language? I'm not so sure.
>That's a straw-man, I said no such thing;
You said:
> The team that makes PHP makes quality a higher priority than just about any other language
Can you explain to me what I misinterpreted?
> And no, I'm not going to provide you with any 'evidence' for my claims, you can take my word for it. This is not a court of law, and besides that I never made any claims about other languages before this comment and I don't feel like putting other languages and/or their developers down.
It's a little silly to make a comparative statement like "The team that makes PHP makes quality a higher priority than just about any other language" and then refuse to provide examples of where this happens and matters. If you'd just said "The PHP devs respond quickly to bug reports", sure, but when you start saying "They do this better than other languages", you're implicitly already "put other languages and/or their developers down". In a case like that, I don't think it's out of line to ask for examples; if there are major failings in other languages that go ignored and unaddressed, then that's certainly worth some attention. If there aren't, the whole point is moot anyhow. If there are examples of systemic problems that go ignored in other languages, I'd like to know about them - that may matter when I choose a language for a project.
I'm not interested in trashing PHP here; I use it on a daily basis, and get a lot done with it. I also use Ruby, Python, Javascript (both server and clientside), bash, and the occassional perl on a daily basis, and don't have problems with mysterious segfaults and unexplained crashes from 2-year-old bugs that would somehow make PHP's lack of such problems a major marketing bulletpoint. I don't understand your position at all, because as far as I can tell, it's "The PHP interpreter is mostly bug-free, and bugs get fixed quickly" (which is great!), but...what language with showstopper bugs survives mainstream use?
>> The team that makes PHP makes quality a higher priority than just about any other language
> Can you explain to me what I misinterpreted?
Yes, that that was terra_t, not me making that claim.
For example, in ASP.NET, I've had a whole team of developers swear up and down that there are no static variables in a particular segment of code that could be the subject of a race condition. Sure enough, I go in there and find one that's hidden away in a very obscure location.
Java has the same problem, as does any environment that exposes threads to the language. Sure, in theory you could do some very nice things by sharing data structures between requests, but practically the whole programming style of PHP encourages people to avoid horrible horrible problems with concurrency, and there are no language features, even in the laboratory that practically make those problems go away.
To take another example, the urldecode function in CPAN has been wrong for 15 years. If you want to write a urldecode that works right, you've got to write your own (possibly using the built-in for a base). Similarly, the URI object in .NET sometimes does incorrect transformations on URIs, and you need to use string URIs if you want to make interoperable semweb systems... PHP has the lowest fraction of 'things that say they work but really don't work' of any web programming languages, and that's the reason why it is so successful. Other languages are "blub" because they don't recognize that it's even possible for things to be this easy.
So yeah, I don't think any sane PHP advocate is going to get in your face about PHP as a superior language. Because it's obviously not true and it really doesn't matter all that much.
Doesn't this come simply from other execution environment? PHP is used for websites and you're very unlikely to need/want threading there. I've never heard django/rails/whatever people complaining about threading (apart from the actual HTTP server people).
On the other hand, Python got popular in research / number crunching (numpy, scipy, etc.) / networking (twisted, tornado, diesel, etc.) -- and the multithreading requests seem to come mostly from that camp.
http://www.reddit.com/r/web_design/comments/bxfok/9_useful_p...
Nice to see that learning a 'crappy' language like PHP got you on to a more even keel in that respect.
A language snob is someone who derives a feeling of superiority from the language they use, in reality the tools matter a lot less than language snobs will have you believe.
I've worked with lots of people over the years, and some of them were all-out snobs about 'their' chosen language.
These people were extremely irritating to work with because they refused to look past the minimal advantage that you derive from working in language a,b or c.
The stuff most of us create could be implemented in just about any language without anything close to the kind of 'slowing down' that you allude to, it all boils down to how well you know your language and the environment you operate in, as well as knowledge about the domain you are building your work for.
In my experience the choice of language has a relatively small impact on the speed of the initial build and the quality of that build.
Over a longer term for less than rocket science style projects it is far more important to have good documentation and a supply of reasonably competent coders than that it is important to do your project in any one specific language.
Of course, there are languages such as brainfuck and a bunch of others that you'd do well to avoid for many reasons but I take it we're limiting the discussion to the top 10 languages used to build everyday applications and web-apps, say Java, C++, Python, Perl, PHP, Ruby, C# and whichever won't spring to mind at the moment.
It can be difficult to tell them apart. But one way is to look at the depth of their criticisms, and how many of those criticisms you've heard before. If they say the same things everyone else says, chances are they're talking out of their ass. But if they can come up with unique, detailed, insightful reasons why a particular language sucks, it's pretty likely that they've used it extensively and have some idea what they're talking about.
PHP does have problems, but it's easy to pick up. I don't know that learning it will make you a better programmer, but a good programmer can definitely make it solve problems efficiently enough.
Try picking one that is as far as possible from the ones you know and then try building something useful in it. That's the fastest way to broaden your horizons.
That said, there's nothing wrong with programmers being picky about languages. Not only are languages what we're forced to think in day in and day out, but they affect what tools we can use, how we structure our code, and (like it or not) what jobs we can easily get.
So no, I'd disagree with your definition of 'snob'.
"a person who believes himself or herself an expert or connoisseur in a given field and is condescending toward or disdainful of those who hold other opinions or have different tastes regarding this field: a musical snob."
Regardless, if avoiding languages I consider inferior make me a snob then I'm happy to proclaim myself a snob.
All it has going for it is that it has low barrier to entry and is a commodity in terms of it's developer and low-end hosting. I consider these as good selling points for PHP but that is all I think it has going for it.
I think the social ecosystem of a language is very important, and I think there's more information and quality libraries in Ruby at the moment, so the popularity vote for PHP is a bit moot nowadays for web application development.
I try not to be a snob, I try to use 'the best tool for the job' but I don't think PHP is a very good tool. And I'm sorry but as someone who uses these tools all day I do not consider it a good tool.
The argument that language choice doesn't matter, it's the engineer that matters, is very valid. I'm an excellent engineer in both and produce high quality working code in both. But python is clearer, more fun, and right.
That was my point actually. :-)
Just because you don't want to use PHP doesn't make you a snob.
And yet, for my own anecdote, at the start of the summer I got a job where the main part of my work is creating a Flex front-end application with Java services running on Tomcat/Jetty for the backend, using SOAP (though we do have REST hooks too generated automatically) as a data transfer protocol. It's kind of ugly at first, since hey, it's Java, but really it's not bad and my "I wish this was PHP/Python" moments grow more and more infrequent.
Python/Perl/Ruby/PHP fill such a similar niche that I try not to go between them, because it would be boring.
My personal plan is to next go Erlang, Perl 6 or some Lisp variant.
Edit: Job offers for 2011 or so would be appreciated. :-)
I figured whatever benefit I got from Python's other 'good' features, I'd lose back with that kind of BS, so I quit programming Python.
If it's really hacked up, then a source code beautifier might be a better bet:
http://www.google.com.au/search?q=python+source+code+beautif...
The alternative is to have wild, all over the place indenting in languages like PHP. Except that the code will still run, so nobody will fix it.
To get a feel for syntax and how PHP works (especially == and reference/value), you should read the docs. They're a breeze and provide lots and lots of examples. The user comments are generally incredibly helpful and will notify you of caveats that you might need to be aware of.
You'll also want to start off by learning how to build objects in PHP, how associative arrays work (they are great), and how to use __autoload() to include classes on the fly.
Personally i'm a PHP dev and love it, sure its got its ugly parts, but what language doesnt. Its a very easy language to learn especially if you have prior coding knowledge, pretty much every single host out there supports it by default, and there is an excellent manual as well as a ridiculous amount of resources for learning it.
Personally i'd view learning any new language as a good thing, as at the very least it makes you a better coder in your own "native" coding language.
If you do however see an old-style "let's just include everything", "global variables are cool", "just shove it into $_SESSION" kind of code... well - good luck, you'll need it ;)
There's also going to be some forehead slapping as you discover what is not possible (like `function_returning_array()[0]` => syntax error)
I get to write PHP once in a while but normally prefer Python. It's easy to spot in the style of the code written... but otherwise, no problems. I'd just suggest changing the error reporting level to maximum - it's not hard to write a lot of PHP code that will both work correctly and report tens of warnings per line.
It also depends on how long BigNameCorp has been around. They might take coding seriously now, but legacy code in general is a pain to deal with. Much less legacy code that's been written in a language like PHP.
That about says it all.
IIRC, I wanted to use Kohana because of some specific plugins recommended to me. However, they were marked as only working in 2.4. 2.3 was in beta, but the consensus I got from IRC people was 'just move to 3 - it'll be released soon and that's where attention is being focused'.
The core code might be well-written, but that doesn't mean it'd be a good project to build on. It might be, but code alone is not the only factor to consider.
It's definitely not elegant when compared to Ruby or Python but it's not as bad as Java. Because you know Python, it won't be hard to pick up but you'll need to accept that you're working with a different environment and live with the tradeoffs.
The neat thing about PHP code is that it runs pretty much anywhere. This is not a big deal when you're building one app that is going to be used by a lot of users but it is a big deal when you have projects you're building that are open source and you want to foster a growing community.
As others have mentioned, a lot of your sanity levels will depend on what their current code looks like. If it's at a company like Facebook, I'm assuming their code would be at least decent or even nice. On the other hand, it could be a complete nightmare to work with bad code. A different way to look at bad code could be as an opportunity to do a lot of work and make yourself very valuable to the team. It just depends on whether you want to take on that type of role.
Now if money and distribution of your code isn't a biggie for you, I wouldn't take the job as you don't want to realize you've wasted a year on something you hate.
The result is a bloody mess. Find a different job.
I guess my point is, when deciding whether this is the right job for you, language is a very important factor. After a little time writing in a language you don't enjoy or respect, if you're like me, you're going to start to feel creatively and intellectually unfulfilled.
(I just learned Python to give App Engine a spin. All my usual languages use semicolons, I finally stuck a little clay on the key to alert my brain when I pressed the semicolon.)
My suggestion is to not pay too much attention to those blogs and focus more on building things.
I'm sure PHP is a good language to build very good websites.
Also, the PHP source code is quite readable, for those times when the good online reference manual doesn't help.
PHP breeds mediocrity in such a superb way, it's rivaled only by Visual Basic. It attracts many dumb people and, chances are, you will meet many of them on your new job.I won't go as far as telling you to stay clear of the job - just have that in mind when deciding.
Both PHP and VB share low barriers of entry. That make them novice friendly, but in the end it encourages practices that everybody else have agreed to call bad habits.
By example, copy & paste. In most languages if you pick a random forum and ask "how do I do X?", you will receive answers that range from "RTFM" to some cryptic link to a Fine Manual to Read. You will be forced by the elders to conceptualize what you want to do, and then learn how does it work. On the other hand, that same request in the PHP and VB forums will get half a dozen of "here is it, dude" responses... including the one form the reading challenged idiot who answered how to do Y instead.
In the mind of the newbie, then, it becomes perfectly acceptable to equate programming to a look up table where there is some arcane incantation that corresponds to every problem.
Not to say that there is no professional people doing quality work in either language. But dealing with the noise in the community is the price you have to pay.
That's simply untrue.
PHP using a decent framework with properly unit-tested and documented code, a team of mid-level and sr devs, version controlled, good comments, a build process, and reasonable development schedules would be a joy to work with vs hacked-up Python thrown together by various people who don't know what they're doing with no process, docs, version control, test, where everything was due 2 weeks ago and people just develop on live servers. That would, simply put, be a nightmare, regardless of the 'purity' of Python.
I recently took on a project based on Rails. I saw some code - there were tests - some of the models I inspected looked decent. My initial view was "it's Rails - how bad could it possibly be?". It was/is horrible. However, once we'd commenced, I realized a) it's Rails1 (and was still being developed in Rails1 as of March 2010), there's 0 version control, no external docs, few internal comments, and 95% of the tests are either auto-generated skeletons only, or at the level of "Add 1 to the inventory, save, then check the inventory has increased by 1" (not checking any logic, just db code).
Parts of the code are - yes - more expressive that they'd have been in PHP. But, overall, the project is an abysmal mess. It's painfully apparent the original dev(s) didn't understand how to build software. They could 'sling Rails stuff together', but that's about where it stopped.
We've made a not too painful decision to rebuild, just using the old code for reference as to how screens should look. Rebuilding is being done in Grails (fwiw), and we're using a bdd approach (easyb) to get started. I was on the fence about using Rails/cucumber, but my skills aren't very strong in that, and we've got a deadline to meet.
I understand PHP isn't perfect. Coming from other platforms, there can be many 'gotchas' which catch people off guard. I've found moving from PHP to other platforms you find similar gotchas. It doesn't make every 'gotcha' automatic proof that PHP is crappy (a few do, but not all!) But again, the language, while important, should not totally define your view of the job or the entire ecosystem.
I suspect that the quality of the code base will affect your happiness much more than the language itself will. I doubt that BigNameCorp belongs to the world where all database fields are VARCHAR(255) and elaborate code is required to ensure that '99' < '100', or where globals float about mysteriously. If so, heaven help you--but they'd probably write Python in the same fashion.
Sure, I'd rather be hacking Python, but I'm not going to go kill myself or anything.