When I got into web development PHP was the default. It's what the web was built on. (To be fair - it seemed like half the web was WordPress). LAMP was the defacto stack - and you were expected to know it.
Since then PHP seems to have fallen out of style hard. I still work on a SAAS product with a mainly PHP back-end, and often have to answer "why PHP?" The truth there is that it's been around since that was the standard and there's no meaningful reason to change it (at least it's not Ruby!) But it got me thinking - are there scenarios where I'd still reach for PHP first today? Yes.
Now I'll still be the first to roll my eyes and say PHP is "awful". It's got no shortage of annoyances. Two of my favorites are:
- The type juggling! Being a loosely typed language is enough to turn a lot of people off in it's own - but PHP seems to make some exceptionally unintuitive decisions. And undefined variables? Meh, let's just throw a warning and give it a try.
- The most inconsistent function names and signatures I've ever seen!
(Go ahead and share your favorite gripes in the comments, we'll all have a good chuckle)
But here's my humble list of things I think are really great about PHP.
PHP was designed for websites. It has the request/response lifecycle built into it's DNA. Query params and request payloads are available in super-globals. STDout is the response by default.
In any other language I've used to build a web site/app I start with importing the right package/library to run a webserver. I don't have to do that in PHP.
Yes, that means I need Apache, nginx, lighttpd or similar. But it also means that I can write code pretty oblivious as to which one of those is in use. It also generally means that each request will be served in it's own thread.
In my experience there's a lot of boilerplate to serve content over http in other languages that's just "already there" in PHP.
Easy to work with APIs/Databases
I do a lot of work in the data integration space, and PHP makes it pretty easy to talk to other services.
Using curl is simple and nicely structured (if you don't mind looking up a lot of flags) and Oauth is a breeze, so REST APIs are pretty easy.
PHP's built in SOAP client is surprisingly good to (considering how much I hate working with SOAP APIs) - just point it at a valid WSDL and every method is defined for you.
Connectivity to most of the popular relational databases comes out of the box - with more available with right drivers.
Also, PHP has been ubiquitous for long enough that msjor platforms offering an SDK for their API will include a PHP variant.
Easy to Hire/Train
Again, PHP has been so popular for so long we have no trouble finding developers who are comfortable with it (even if they say "well I prefer python but...)"
But I've also had several people who are right out of college - which seems to mean they know either Java or C++ and for some reason CSS (weird mix... I'd like to see these curriculums). I've never had much trouble getting them spun up on PHP.
Some folks will point out that the ease to jump into can be dangerous (a lot of people trying to walk before they run - and with the aforementioned loose types). That's true, and it's a way JS and PHP seem similar. But in a setting with good mentorship it's been an asset to us.
So when should I use PHP?
Here's a few examples of times I would reach for PHP first.
Writing a RESTful API.
Writing a back-end that needs to interface with a lot of different platforms and APIs.
Any web app that needs to serve dynamic content from a DB but does not require any real-time interaction between users (like a blog or a forum).
An app that I want to be easily "moddable" or have a "plugin ecosystem". Though NodeJS is a good contender here to.
So... Gimme your thoughts! What did I miss that you love or hate about PHP. Is it dead? Let me know. ; )