Saturday, February 05, 2011

PHP vs Python: A followup

I received a lot more commentary for my previous post than I anticipated, so I thought I'd followup with some comments and feedback I received.

PHP not dead, just out of use
First, the title of my previous post included the phrase "dead to me".  I in no way wanted to imply PHP was worthless to anyone and its large community was going away.  I honestly don't care if PHP sticks around forever.  My point is that as templating libraries for the web have gotten so good, much of the code is outside the template and the small benefit of embedding PHP directly into html is much less useful than it used to be.  www.php.net's first description for those who might be new to the language are immediately shown, "PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML."  Within the first sentence they advertise PHP as being html-embeddable.  My point is, big woop.

PHP's gotten boring
Many people have commented on other forums about leaving PHP, because its gotten too boring.  And vice versa, leaving another language like python because it too got old and went back to PHP.  Whatever floats your boat, I guess.  In some ways arguing about the pleasure of programming in PHP over python is like arguing the merits of Spanish over German.  Clearly, German is superior.

People tend to have relatively strong opinions over rather mundane things ranging from favorite foods to their preferred brand of socks.  Programmers similarly can have very strong opinions over their languages of choice when sometimes it's their our to express themselves.  Reading blogs comparing lines of python to lines of ruby tend to put me to sleep.  Some comments on this subject included the ugliness of php or python.  Ugliness, of course, is a very subjective word.

Years ago I encountered a site railing against the java language.  I found most of the author's points, however, to be mostly an issue of taste.  One issue he mentioned was the case-sensitive nature java required/assumed.  This perplexed me seriously as almost every language I've ever used in my life was case sensitive.  Indeed, jumping over to TIOBE, it looks like the top ten languages all have case-sensitive syntax.  To OO developers, this style is often used to easily distinguish between class and object.  Calling java ugly for using a industry standard?  That's like calling a girl ugly because she's not a blonde asian.  My point is what exactly is ugly about python?  To me calling a programming language ugly demonstrates an inability to express one's possibly valid thoughts on a language.  For example, some perl I might considered "ugly" for its compact, symbol-based syntax and parameter passing.  Some find python ugly simply for lacking the C-style angle brackets between blocks--are they by contract considered beautiful?  One might call java ugly for its extreme use of compositing just do simple file I/O.  Or are these people literally calling some language visually displeasing?

So beautiful...

PHP's Community
One of PHP's biggest strengths is its availability across a large range of hosting providers.  PHP currently is the standard web language any hosting service must provide.  Hosts providing alternatives such as python and ruby-on-rails usually are a little harder to come by, which is a boon for PHP.  One commenter strongly disagreed with my statement of the python community's size compared to php.  The community can mean different things, so I'll admit it my mistake.  Again, looking at the TIOBE data, PHP is shown to be more popular than python (though the latter looks to overtake it next year).  Or you can simply google "php community" and see it garners more than 77 times the hits as "python community".  Maybe python programmers are anti-social...

Long live templating engines!
Concluding with my previous post's title, templating engines have replaced my "need" of PHP.  PHP isn't horrible by any means, but with capable templating engines in so many languages, anyone can just program in _insert_favorite_language_here_.  In this case, PHP is just another scripting language.  I thought about porting blocks of PHP code to python for readers to compare, but after a brief perusal I found the endeavor totally pointless considering the relatively little changes I'd make between them.  So instead of ending with a quip, I'll end with a question: Supposing a script-savvy developer was entering web development, what are some motivations for using PHP I haven't mentioned?

Wednesday, February 02, 2011

php to python: Why PHP is now dead to me

PHP started in 1995 and was a well-known web language by the time I was getting into web development around 2001.  At that time I had just picked up perl--yes, perl, which at the time seemed like the end-all, be-all sexiest language ever.  The only thing I liked about PHP, which I admit was a very big draw was the seemless fusion of content and code.  With PHP's delimiters, one could easily blend between the content and wiring.  Almost ten years later, though, PHP is starting to show serious age to the point of being a deprecated tool in the toolbox (like no one making Phillips screws anymore). 

PHP as a web language
My first grudge against PHP is it's not a very generic language.  People have successfully written non-web-based PHP applications, but this seems to be more of a PHP programmer, who doesn't want to learn a more robust language. After a brief scan of various PHP support sites, PHP development outside the realm of web development is basically negligible.  I see no reason, in fact, besides some amazing library I might not be aware of, for me to use PHP for a non-web-based application. 

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.  -http://www.php.net/

The syntax is ugly, in my opinion: requred semi-colons, arrows instead of periods to reference data, global functions to manage things like arrays instead of a more object-oriented approach, etc.  After several iterations of the language, features seem tacked on to the early implementations.  With python being faster and providing a larger development community, what motivates me to use PHP? 


The MVC architecture
The MVC architecture is a design philosophy of separating the application into three sections: the models, which manage the data of the application; the views, which typically represent the user interface of the application; and the controllers, which handle a lot of the management between the views and models.  While this design can be strictly conceptual to a programmer separating out functionality, various frameworks like Qt and CodeIgniter strictly separate these entities.  Here's where I believe php has lost it's magic.

The main allure of PHP that stole a lot of perl's magic was putting the controller elements doing the processing directly into the html.  Instead of managing large perl files mixed with html, people could make a mostly html-based file with a little PHP, which was much cleaner to read.  In that sense, all the view implementation was usually separated from the model and controller.  This seemed great at the time, but as MVC has gotten more prevalent, more and more PHP frameworks remove model/controller components from the html leaving small bits and pieces in its place.  In my experience, and what seems to be the practice now in PHP, large controllers and models are created completely separate to the actual html.  This creates a more coherent architecture, but it raises the question: now that we've moved so much PHP code away from html, why are we still using PHP?

The rise of templates
Templating libraries have been around for many, many years-even before PHP was popular.  However, since some programming language had to drive the application anyway, I believe, there was no reason to use one if one had to do his/her coding in C/C++ or perl still.  Now, however, many new languages have emerged such as ruby and python that provide a very small and more robust scripting experience.  Because they are so general purpose, many more examples exist outside of web development.  This is important as web applications have continually provided more and more functionality than just serving up semi-static web pages.  The little bits of php code that was once in my html files are now easily replaced with some templating library like mako or genshi, and I can program in a more general-purpose language, whose techniques will apply to non-web-development tasks as well.  Again, now that PHP has been moved outside of html, why should I use it over python or ruby? 

Things I'll miss
Alas, PHP, I hardly knew thee.  One thing I'll slightly miss, though, is the startup time in PHP.  Almost every apache configuration on Linux typically comes with PHP configured.  After opening up a .php file and typing a few lines, I can access the file directly in my web browser and I'm immediately seeing the results.  With python, I have to do a little bit more typically to get things running via apache.  This requires setting up a cgi-bin, creating a proper script alias, etc.  Newer python frameworks like web.py and turbogears now come with a mini-web-server included, however, so I can hold off on configurations until I'm farther in to production.

I'll also miss PHP's documentation, which I believe is generally more accessible than python's.  Many of PHP's functions get an entire page dedicated to them like explode().  The page features the basics you'd expect from an API doc--parameters, description, and return value--but includes a huge swath of examples of how to use the function.  It's like an entire programming cookbook dedicated to that function.  I find a lot of python docs to be a little too wordy (ex. datetime and difficult to browse to a particular function--especially if I'm searching by functionality and not name.  If I'm unfamiliar with a language, I'd like to know what functions are available for a given class before treading through each and every description.  The function name should typically provide sufficient description to investigate it further.  Anyway, the PHP wiki/comment style of documentation is something I hope more languages adopt.  Wikis are already prevalent on many projects, but it seems people are much more willing to contribute through a brief comment box instead of stepping into an entire editing mode.  Is it fear?  Laziness?  Regardless, it seems more accessible to contribute something beneficial to the doc.

Anyway, PHP.  Thanks for everything.  It was fun while it lasted.