PHP has gotten a bad rap in the past few years, especially from the Rails community.  It’s often seen as the poor-man’s scripting language that should never be used for anything serious. In many ways, I think this is an unfair reputation and this series, called “PHP web design,” is designed to show the benefits of PHP and explain best practices. PHP certainly has a plethora of limitations, don’t get me wrong.  I’ll be exploring the good and the bad as we delve into the depths of PHP.

The bad

Lets get the bad parts of PHP out in the open.  There are many more flaws than I’ll list here, but for the sake of brevity I’ll list the ones that I think are the worst.

  • Horrible memory management. If you ever need to maintain and process large arrays of data, you’ve likely hit memory limits in PHP.  I ran some tests using the memory_get_usage() function.  A script that simply creates an array variable takes 93 KB of RAM.  Storing 1000 integers in that array takes 290 KB.  Storing 10,000 integers takes over 2 MB.  And that’s just storing integers.  Complex objects take much more space.  The overhead for each element in PHP is enormous.
  • Lack of internal consistency. I’ve worked with PHP for over 10 years, and the fact that I still have to look up the parameter order for str_replace() really says something.  For string manipulation functions, sometimes the haystack is the first parameter and sometimes it’s the last.  There is very little internal consistency or convention.  This is because PHP evolved over time, rather than being designed.
  • Can you say, “slow?” If you have a long-running complex algorithm, you can expect it to take an order of magnitude longer in PHP than with most other scripting languages.  Scripting languages in general are slow, but PHP seems to be exceptionally slow.  I usually offload complex tasks to Perl or Ruby.  Depending on the task, I sometimes offload to C to squeeze out every ounce of performance possible.
  • Bad design is easy.PHP does not promote good design.  It’s very easy to hack a website together and have an unmaintainable jumble of spaghetti code before long.  It takes discipline and consistency to create a well-structured PHP site.

The good

For all its flaws, there are many reasons that I think PHP is still a viable solution for websites and web applications.  Here are some of the best things PHP has going for it.

  • Simplicity.PHP is simple and easy to learn.  The code is straightforward and easy to read (assuming the site has been properly structured).  It only takes a matter of minutes to get a fully working basic site structure in place.
  • Prevalence. PHP is everywhere.  It is included in all Linux distributions, and is available on most web hosts.  You don’t have to look far to find a server with PHP enabled.
  • Minimal server requirements. Basic PHP is very easy to install without any fuss.  It can be compiled with very few dependencies.  All you really need is a web server like Apache, and you can have PHP up and running in a few minutes. If you’re using pre-built packages, it’s even easier.
  • Amazing centralized documentation.I have rarely seen better online documentation than what is available at php.net.  This documentation has been up and maintained for years, and user comments are invaluable.  The documentation is easy to read and the functions can be understood in a matter of seconds.  They all contain great examples, and the ones that don’t are provided by user comments.
  • There’s a function for that. Say what you will about the lack of consistency and convention, but there are built-in functions for almost everything with PHP.  This is very handy once you get to know them.
  • Beautiful object-oriented features. Yes, I said it.  PHP 4 had awful object oriented functionality, and few realize the depth of change that came with PHP 5.  I have developed some rich APIs with PHP 5 that take full advantage of abstract classes and interfaces, as well as complex subclassing. The object oriented capabilities of PHP 5 are beautiful, to say the least.

Concluding thoughts

Yes, PHP has its flaws and we are all aware of these flaws.  But major sites like Facebook still use PHP.  I’m running NeoBudget on a 256 MB slice on Slicehost and haven’t come close to running out of RAM. I believe PHP can be a good choice for creating a website or web application. This series will explore good PHP programming practices and will hopefully help rid the world of the stigma that has been associated with PHP.

Don’t agree with me? Feel free to heckle me in the comments.