
Ask HN: PHP for Python programmers - reinhardt
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.<p>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.
======
viraptor
If `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.

~~~
jhherren
Array dereferencing in is the trunk and will be available in 5.4.

~~~
damncabbage
And will be available for use on BigNameCorp's RHEL servers in about eight
years.

~~~
j_baker
If then. Look how many BigNameCorps are still using ASP classic.

------
j_baker
_Can_ 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
Perhaps 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
Is 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+beautif...](http://www.google.com.au/search?q=python+source+code+beautifier)

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.

------
mgkimsal
I 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
In 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.

------
SapphireSun
Don'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.

~~~
jacquesm
Language snobs, as a rule, are people that have not programmed in more than
one language, or that somehow think that their chosen superior tool makes them
superior as well.

Nice to see that learning a 'crappy' language like PHP got you on to a more
even keel in that respect.

~~~
jules
On the contrary. Language snobs have programmed in multiple languages. That's
why they can clearly recognize a flaw when they see it. If you've always used
a tool with the same flaw you likely don't recognize it as a flaw and just
think that that's the way it works. Or you do kinda recognize it as a flaw but
don't realize how much it slows you down.

~~~
nostrademons
I think there's more than one kind of language snob out there. Some of them
have never programmed in more than one language, and so naturally think that
language is the best. Some don't regularly program in _any_ language, and take
whatever fad du jour is on Reddit as gospel. But others have programmed in
lots of languages, and have tried the languages in question, and _that's_ why
they think it sucks.

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.

~~~
mcpherrinm
Well put. I've seen many people who will be quick to jump and say
Java|C++|PHP|Ruby|Python|C# is the BEST AT EVERYTHING EVER because the
Internet tutorial they took told them so. These people are so common that
having opinions on a language is something that you actually need to be
careful what remarks casually about, lest you be branded an uninformed idiotic
language bigot. Most languages in wide use are there for a reason, but that
doesn't mean any and all opinions on them are invalid.

~~~
nocipher
I'm not sure if it's an oversight or intentional but you didn't mention Lisp,
which is, for many "snobs", the language of choice.

~~~
xyzzyz
He should have mentioned Lisp, along with some other languages. However, I
don't know how intentional that was, but he only mentioned languages used by
the first kind of snobs -- those who are not familiar with great number of
tools. Of course, I'm oversimplifying, but I'm sure that average Lisp,
Smalltalk or Haskell programmer have used more languages than average C++,
Java or PHP.

------
jrockway
If 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.

------
melvinram
I'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.

------
hdeshev
PHP 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
Completely 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.

------
indigoviolet
Atleast at Facebook, the PHP code quality and abstractions are very very good,
and getting better. Developing the compiler in-house has some advantages.

------
jws
You 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.)

------
photon_off
Switching 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
Magic quotes is also worth a mention. It's bound to throw anyone coming from
another language for a loop.

~~~
photon_off
Magic quotes has been disabled by default for several years. All you need to
know about them are: disable them if enabled.

~~~
duskwuff
And 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.

------
bendmorris
I'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.

------
terra_t
The 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
Seriously? 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
If 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
I'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.

~~~
jacquesm
I've filed bug reports for other languages and have never so much as received
a confirmation, let alone a patch, the php devs responded within a few hours,
that shows to me they care.

> 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.

~~~
cheald
> I've filed bug reports for other languages and have never so much as
> received a confirmation, let alone a patch, the php devs responded within a
> few hours, that shows to me they care.

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?

~~~
jacquesm
> 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?

Yes, that that was terra_t, not me making that claim.

~~~
cheald
Doh. Egg all over my face. My apologies!

~~~
jacquesm
np. Glad we got that cleared up :)

~~~
AgentConundrum
This is why I love HN.

------
code_duck
It's pretty simple. Just switch off your brain and pretend you're writing
really poor JavaScript.

------
neovive
The Kohana PHP framework source (<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
Really? 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.

------
trop
Think 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.

------
Kaizyn
Programming 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
Your 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.

------
julius_geezer
Bookmark <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.

------
Gilson_Silveira
When 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.

------
xiongchiamiov
I 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.

------
ramy_d
not that I've written any python, but is PHP that much low level?

~~~
steveklabnik
PHP 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.

------
smitts
You'll be be fine. I've found it very easy to switch from/between
C/C++/PHP/Python. Perl on the other hand...

~~~
berntb
C++? 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
Depends what order he learned them in. Maybe he already knew syntax from C and
classes from Python before learning C++.

------
mkramlich
I'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.

