so, why is php so popular?
hacking
php is a typical "hacker" language - it's easy to get started on simple scripts and rapid hacks. it takes a lot of discipline to stay organised and clean when it comes to big projects, but it definitley is possible. that's because of phps heritage - it started as a very thin layer between the database and the webserver. often functions have incoherent naming conventions, because they once were wrappers for the native unix functions, mimicing their names. one example of this is array_merge
versus sort
(instead of array_sort
).
another thing is the mixing of php code and html. on the one hand thats an advantage - for small, manageable scripts. it is important to separate those two in bigger projects. you can do that with templating (e.g. smarty
) and discipline. some frameworks enforce this, so you're not tempted. a problem? only if you don't know better or have no discipline.
a lot of programmers have a low opinion of php and web programmers, and they are mostly right. html is easy (by design), everybody can do it. the next step after learning html (and a little bit of javascript hacking) is php - and php makes coding very easy; it's the beginners language. so there is a huge crowd of amateurs with no understanding of the underlying principles of programming, algorithms or even the http protocol, calling themselves "web developers" and hacking together websites that are badly designed, unreliable and insecure. the barrier of entry is just incredibly low, and there are virtually no costs (you don't need books, IDEs or other fancy stuff to start). php doesn't enforce anything on you - do as you like. great for trying, learning, playing!
php is popular and well liked - by the amateurs. that doesn't mean all experienced programmers dislike php (many do - even those who use it every day -, because php is just not elegant). it still has certain advantages ...
strict typing versus loose typing
loose typing is not a penalty per se. often, it can greatly increase your productivity, because you don't have to manage type conversion manually. especially in web developement, in the end the input and output are strings most of the time. in a typed language you would have to cast-to-string or cast-from-string permanently.
<?php $string1 = "44"; $string2 = "35.4"; echo $string1 + $string2; ?>
just works as expected (79.4) - at least through the eyes of a beginner (who doesn't understand the intricacy and pitfalls of types, loose typing and automated type conversion).
yes, it introduces the possibility for type errors; but very often they're handled gracefully or you can catch them with automated unit tests. a lot of (well known) programmers think that loose typing is not a problem for big projects if you (unit) test your code accordingly.
interpreted
no compilation cycles, whooo! code, test, deploy, code, test, deploy! rapid developement, rapid deployment!
hosting, lock-in and distribution
- it's easy and cheap to get php-enabled hosting.
- a lot of projects are already hosted with php, and often people don't want to change servers when updating or redesigning.
- other programming languages may be more elegant, more modern, simply better (but be careful, my language is better than your language is like smoking while bathing in a pool of gasoline). but as a customer, if you already got a website running on php it is often problematic to install an addon (e.g. a forum) in ruby, python or serverside javascript. even, if 60% of websites use php and 0.7% use, say,
rebol
as a backend language, and you want to sell your product ...
maintenance and lock-in
because there are so many sites in php, you need php programmers for updates and bug-fixes. additionally php programmers are cheap (pun yay!), because there is a huge market.
developer lock-in
if you work at a web design company that used php since the dawn of time (because the founder was a typical php-amateur herself) it's really a pain in the a$$ to convince anybody to switch. almost all of your collegues are typical cheap php amateurs. almost ALL of your (legacy) codebase is php. many bosses think it's too much of a risk switching to another language. even google allows only a small pool for server side developement.
steve yegge:
The thing is, Google's decision to limit the number of languages allowed for production work is actually pretty smart. It's one of those things that might sting for a few weeks after you start (and not to put too fine a point on it, but why aren't you sending me your resume?), and if you're a bumbling fool like me it might take you a little longer to figure it out. But programming languages have a core of standard functionality that everyone knows, followed by a long tail of murky semantics that become increasingly difficult to figure out, especially for dynamic languages with no actual spec like Perl, Python, Ruby and their ilk. Google very prudently keeps the number of languages as small as practical, so that we can build up large groups of experts on the semantics of the languages we've chosen.
It's also to cut down on the combinatorial explosion of components required for language interoperability -- a huge tax at other companies I've been at, a tax that Google has managed, by and large, to minimize.
libraries, code snippets & community
- every problem you encounter in web developement has already been solved by some other guy who posted the php code or mentioned it somewhere (very often in the php documentation comment section itself). copy and paste hacking, again.
- all the big tools and services you may use have connectors for php. there are 21 (!) supported database systems mentioned in the php doc)
- APIs: if someone designs an API, she most likely includes a tutorial how to use it with php.
- extensions: if you need something to be really performant (at least, faster as native php could be), you can always create an extension in c.
essentially, php has got everything you need to build (big) applications (with the exception of namespaces maybe, but those are coming in the next release).
documentation
the php documentation is great, period.
performance
lets face it: php is old (at least in terms of the internet). this brings disadvantages, like the before mentioned incoherent naming they just can't get rid of for compatibility reasons). on the other hand, the interpreter is well tested and optimized. php may not be as fast as other languages, but it is fast enough 99% of the time.
IDEs and editors
there are several useable IDEs available (eg. php-eclipse, zend studio, php-netbeans, ...) but you can also use simple text editors without much hassle. when was the last time you used notepad to quickly fix something in java?
compatibility and developement
- the php5 interpreter is (mostly) compatible with php4, so you can run all the old legacy apps and even mix it with new code.
- php gets frequently updated, new features are added constantly. the SPL, for example, tries to offer more experienced programmers an alternative to the legacy naming problems by reimplementing lots of the old, messy stuff in a clean new object oriented manner. hey, even first class functions and namespaces in 5.3! this language is pretty alive and evolving.
javascript and php
they don't have much in common, other than being interpreted, loosely typed languages primarily used in web developement. together with html and css (and maybe mysql) they form the trinity of web developement*. i don't have numbers to prove it at hand, but i think it's still true.
javascript is most often used in the browser, but is also available as a serverside scripting language (mozilla rhino, experimental google mod_v8).
is more elegant and modern, supports first level- and anonymus functions and has a different object oriented design paradigma (prototyping). javascript is fast, because the browser developers have a my-peni..., i mean, my-interpreter-is-better-than-your-interpreter argument at the moment, with JITting to native machine code and everything.
(* i know, there are perl, asp, python, jsp and (runner-up) ruby (because of ror). does anyone have numbers?)
i use php on a daily basis for more than 10 years (almost half of my life!). i had (mostly superficially) contact with lots of languages, including assembler, c, server-side javascript, java, haskell, prolog, ruby, python and countless more (but not perl, thankfully!), so i know a bit of the world outside of web developement. and i wouldn't mind leaving PHP for a more elegant and modern language like ruby or ss-javascript ...
but it's just not possible (ok, just very, very hard!). it's everywhere! and even if my boss allowed another language, i'm most fluent in php; so for really critical and complicated apps, i'd probably come back to PHP anyway.
summary
essentially, the reasons ordered by percieved importance are ...
- ease of use, easy learning, low barrier of entry
- size of the community and market, including lock-in
- ease of deployment (hosting)
- it's good enough, so why change?