7 ms·
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.
- ramy_d 16y agonot that I've written any python, but is PHP that much low level?
- steveklabnik 16y agoPHP isn't super low level, but it is for a server side language. Much of its standard library is just a pretty thin wrapper over the C, and since it's often used without a framework, the code 'feels' lower level.
- mkramlich 16y agoIt feels a little bit lower-level than Python, in terms of the runtime, rather than the language. It also struck me as being lower quality in terms of their runtime, and their docs, than Python and several other languages I've used in recent years. Closest to amateur hour of all the languages in widespread use on the web, in my judgment.
- code_duck 16y agoIt's basically the equivalent of Python, Perl, Ruby, etc. It completely fits into that same niche as an interpreted dynamic language with OO capabilities. It's nowhere near as elegant as Ruby or Python, but similar concepts can be expressed with +-35% lines of code. So no, it's not actually low level at all. You don't have to do memory management. You don't have to declare variables. Arrays are not fixed sizes. It is compiled to intermediate bytecode... etc.
- terra_t 16y agoThe team that makes PHP makes quality a higher priority than just about any other language; if something is built into PHP, it's pretty likely to work correctly. (For example, PHP and Python have the exact same problems with threads, and the PHP team says "don't use PHP in a threaded environment" and the the Python people say "come jump in the water is fine") It'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.
- j_baker 16y agoSeriously? Don't get me wrong, I could imagine valid reasons to use PHP. But the quality of the language itself is at the very bottom of that list. I don't usually hear PHP advocates making that case. Usually it's a combination of "the language isn't that bad" and "there are a lot of libraries for PHP".
- jacquesm 16y agoIf you define quality as 'orthogonal naming of functions' and 'elegant constructs' then yes, PHP has its flaws. 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.
- cheald 16y agoI'm not sure I understand what your argument is. That there aren't major showstopper bugs that make PHP unusable due to various segfaults? Sure, that's true. But do you mean to suggest that such things exist in greater quantity in other such languages? You say the PHP devs have a sharper focus on quality than other languages; can you provide and kind of evidence for those claims? Not just good response time by the PHP devs, but systemic failures in other languages that go unaddressed, as well? 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.
- SapphireSun 16y agoDon't worry about it. I was in a similar position a few months ago when I, a python programmer, was offered a perl position. Eventually you just get used to it, and you might even find things you like. I've stopped being as much of a language snob. Some features and most syntax are just decorative, and you'll adjust. The whole giant namespace thing would bother me a lot, but hey its worked for other people before.
- j_baker 16y agoJust because you don't want to work with a language (especially PHP, dunno about Perl) doesn't make you a language snob. If you're closed off to any languages other than your preferred language, you're a language snob. 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.
- corin_ 16y agoSnobbery is defined as looking down on anything you consider inferior - for example in social classes, you can have middle class snobs without them thinking that every other class (including the upper class) are worse than them. So no, I'd disagree with your definition of 'snob'.
- j_baker 16y agoDictionary.com seems to see my point of view as being valid: "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.
- ryan-allen 16y agoThat is good advice, but what will PHP teach someone exactly? I don't believe it has anything interesting to offer someone who has learnt a number of languages. 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.
- smitts 16y agoYou'll be be fine. I've found it very easy to switch from/between C/C++/PHP/Python. Perl on the other hand...
- berntb 16y agoC++? Strange. It took me time to learn seriously. I used to reread "Effective .* " every few months. 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. :-)
- metamemetics 16y agoDepends what order he learned them in. Maybe he already knew syntax from C and classes from Python before learning C++.
- j_baker 16y agoIt's easy to write code that works if you're switching between C, C++, PHP, and Python. However, being able to write idiomatic code is a different matter entirely.
- j_baker 16y agoCan you learn PHP and be successful with it? Absolutely. Do you want to? As a Python programmer who's had to do PHP from time to time, I can say it isn't terribly fun. If I were to make a decision based solely on the programming language though, I wouldn't choose a shop whose main language was PHP. Again though, that's assuming that I was just basing it on language. I've found choice of programming language to be one of the less reliable indicators of how good a company would be to work for.
- terra_t 16y agoPerhaps I'm irrational, but early in my Python career I got back a source file from a friend that had invisible changes in the spacing (tabs and spaces got confused) and spent entirely too much time fixing the resulting bugs. 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.
- anthonyb 16y agoIs it really that hard to search and replace a tab with four spaces? %s/\t/ /g should do it in Vim. Make it /gc if you have tabs in your code for some reason. 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+beautifier 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.
- photon_off 16y agoSwitching languages, for me, has usually been a matter of remembering syntax, some minor differences (such as how arrays work, what things are built-in, etc), and the patterns in each language that naturally arise from those two. Of course, things get more complicated if the language itself is structured differently (eg: Javascript), but there are a lot of underlying concepts that carry over (scope, stack, OOP, etc). In short, it just takes practice. If you're "advanced", you should be able to pick up on it pretty quickly. 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.
- cheald 16y agoMagic quotes is also worth a mention. It's bound to throw anyone coming from another language for a loop.
- photon_off 16y agoMagic quotes has been disabled by default for several years. All you need to know about them are: disable them if enabled.
- duskwuff 16y agoAnd if you can't guarantee they're disabled (e.g, if you're writing code to be used by other people), undo them at runtime as early as possible.
- indigoviolet 16y agoAtleast at Facebook, the PHP code quality and abstractions are very very good, and getting better. Developing the compiler in-house has some advantages.
- dreadlabs 16y agoI think one of the main things is to go into it with a more positive attitude, i'm not criticising you or anything, but you seem to already view PHP with disdain. 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.
- viraptor 16y agoIf `BigNameCorp` writes a lot of PHP seriously, you won't notice that many problems. It's not going to be PHP anymore. It's going to be Symfony, Zend, Cake, or something else (maybe even some proprietary, internally used framework). It isn't that easy to find pure-PHP code these days - it just takes too much time to write. 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.
- j_baker 16y ago"If `BigNameCorp` writes a lot of PHP seriously, you won't notice that many problems." 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.
- jhherren 16y agoArray dereferencing in is the trunk and will be available in 5.4.
- damncabbage 16y agoAnd will be available for use on BigNameCorp's RHEL servers in about eight years.
- j_baker 16y agoIf then. Look how many BigNameCorps are still using ASP classic.
- kennethreitz 16y agoPHP has 3079 built-in functions. That about says it all.
- neovive 16y agoThe Kohana PHP framework source (http://github.com/kohana/core http://github.com/kohana/core) is a great example of well-written PHP code. You can definitely get a sense of the "good parts" of PHP from reading through the code.
- mgkimsal 16y agoReally? Perhaps I just looked at the wrong time, but I was looking at Kohana last year and ran far away. The issue wasn't the core, but everything else around it - the plugins, the dev attitudes picked up from mailing lists, the release process and more. 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.
- code_duck 16y agoIt's pretty simple. Just switch off your brain and pretend you're writing really poor JavaScript.
- melvinram 16y agoI've had to use PHP even though I really love Rails because WordPress is built in PHP and a lot of my client work is with WP. 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.
- jrockway 16y agoIf you're like me, you'll never be happy using PHP. It's like being a piano player and having someone ask you to play their favorite song, but only if you cut off 9 of your fingers. The result is a bloody mess. Find a different job.
- bendmorris 16y agoI'm also a huge fan of Python, and I do believe that it (or other higher level languages like Ruby and Lisp) spoils you. I once took a well-paying job, but the main language used was ColdFusion - and not just ColdFusion, but a huge, complex, disgusting ColdFusion framework that the company had written themselves and was riddled with bugs. I figured I'd be fine because of the paycheck and the prestige of the job. Switching to a new language wasn't hard or anything, but I ended up only lasting a few months, then quit, taking a significant pay cut to get away from ColdFusion. I found that I just couldn't stand the stupid language. 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.
- jws 16y agoYou are going to have to learn what the shifted '4' and the unshifted ':' keys on your keyboard do. You might start practicing to build up those muscles. (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.)
- Gilson_Silveira 16y agoWhen I started programming I was using Visual Basic and Pascal. I miss those days because I was focused only on building things and was not aware about "Which is the best language" blog posts. 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.
- trop 16y agoThink of PHP as a really amazing shell scripting language. Just as shell scripts can be glue for all the powerful UNIX utilities, PHP gives you APIs for just about any useful library you might need for web work. You won't be able to use your fancy Python chops for things like generators or itertools, but with luck instead you'll be running a bunch of powerful well-designed existing C code with a few function calls. Of course, a significant project will require a bunch of data & logic in PHP... Also, the PHP source code is quite readable, for those times when the good online reference manual doesn't help.
- mkramlich 16y agoI'm a Pythonista who had to deal with a client's legacy PHP codebase earlier this year. One tactic I used was to try to write all new stuff, where I could, in Python. Tasks that could be separate processes from their existing PHP stuff. For example, command-line tools, automation, supplementary web apps, etc.
- hdeshev 16y agoPHP is an okay language. I'd never choose it by myself, but most of the time it is bearable. The language is not the problem though - it's the people. 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.
- crpatino 16y agoCompletely agree! 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.
- Kaizyn 16y agoProgramming languages are tools of the trade and no different than tools in any other field. If it's a good job then it is a good job. If the job requires you to work with PHP, then that is a lesser consideration that is barely worth mentioning. Good coding can be done in any language even crusty COBOL or LISP. After you learn the PHP syntax, then you won't think about it anymore. In a few weeks time, you'll be surprised that you were worried about something so trivial as the programming language used.
- xyzzyz 16y agoYour post suggests that the only people who care about what language they use are the ones who did not use any languages other than the one they like the most. That's simply untrue.
- mgkimsal 16y agoI realize the question was about language specifically, but something that wasn't really addressed in all these comments is the totality of the environment PHP will be used in. 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.
- zeemonkee 16y agoIn the same boat right now with a Django project and couldn't agree with you more. Django is a decent framework but no framework or language will prevent a crappy mess if you're determined enough.
- julius_geezer 16y agoBookmark http://www.php.net/manual/en/ http://www.php.net/manual/en/. 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.
- xiongchiamiov 16y agoI started as a PHP programmer and transitioned to Python. I'm currently doing PHP at work, and it's not too bad, despite some of the warts that come with an application designed over 5 years by a single person. Sure, I'd rather be hacking Python, but I'm not going to go kill myself or anything.