Thursday, February 23, 2012

PHP vs. ASP.NET - An Expert's Opinion

I've seen many comparisons on the Internet between PHP and ASP.NET, however none coming from the perspective of an expert in both languages. I will attempt in this article to explain the pros and cons of each language and then come to a conclusion about which is better.

Firstly, I started off as a assembly language programmer way back in 1983. I then moved to C in 1985 and then to then to C++ starting in 1992. I did not become a serious C++ programmer until about five years ago. I have, since 2001, programmed equally between C++, C# and PHP. I have taken on some serious projects with Silverlight, WPF, VB, VB.NET and MS Access as well. My SQL skills are fairly good as well, and can pretty much hold my own among the experts. I briefly became a full time PHP programmer back in 2002, migrating from pure HTML with some javascript. PHP was the logical choice, as it was superior to ASP and Perl (which were the only other practical choices at the time.) I wrote many websites during this time.

Recently, I became a part time PHP programmer on a much more complicated project. I program as well in C++ and C# (WinForms,WPF,ASP.NET) during this time. I have had to spend a tremendous amount of time on Javascript as well. I could write many articles on how horrible Javascript is, but that is not the point of this article.

Switching between the two languages, PHP and ASP.NET is very interesting.

PHP has the following obvious advantages:
1. It runs on any platform without the need for compiling.
2. It has a large library of relatively easy-to-access functions.
3. It is C-like, so it's not hard to use.

ASP.NET has the following obvious advantages:
1. Once compiled, runs very quickly and normally runs several times faster than PHP.
2. No significant performance hit for things like comments and abstractions.
3. The editor and debugger are very advanced.
4. C# and VB.NET are both extremely powerful object oriented languages with such features as Linq.
5. Code can be shared between over .NET projects, such as WinForms, Windows Services, Silverlight and WPF applications.

PHP has the following disadvantages:
1. It runs extremely slow compared with ASP.NET. Heavy use of caching is made to speed PHP up. However, not everything can be cached, such as searches.
2. Can not be easily extended. One must compile a module into an extension.
3. Editors are not as good as ASP.NET editing and debugging.
4. Performance hit for abstraction and documentation.

ASP.NET has the following disadvantages:
1. Runs on Windows only (Mono can work on Linux, but too limited to be practical).
2. Must be JIT compiled (only takes a hit the first time it runs.)

So, the question still remains, which one is better? Which is the clear winner? In a perfect world, I would chose WPF and C# with dabbling into C++ as necessary. However, we are not living in a perfect world and the reality is that many requirements are for cross-platform, browser based applications. And this is the reason that PHP is still more popular than ASP.NET.

But is it better? Better in every other sense.... no. PHP, may work very well for small projects and simple code, but once the requirements start to become complex, PHP becomes extremely problematic. Dynamic types, despite what people say, are sometimes extremely annoying and sometimes you want a compiler to find errors before you get into testing.

With ASP.NET, I can abstract functionality, and separate it into well defined modules and take no performance hit for doing so. With PHP, I must break classes into separate files and load the entire file, even if I only want one function in the class.

I also take a small performance hit for every comment I make in PHP. In ASP.NET there is no such problem. However the pressure to optimize PHP, makes it relatively unreadable and with dynamic typing, it becomes very hard to enforce rules in code.

But really to measure performance of a language, I look at how much longer something takes to do in one language than another, and then how long it takes to maintain. ASP.NET, in general develops faster than PHP and I can share code with my desktop application or service. I like that I can organize ASP.NET better. Although it takes more time to organize, it pays off with a shorter maintenance time. People understand my code better when written in ASP.NET. The editor is also superior to any PHP editor on the market (free or commercial.)

The one big annoyance with ASP.NET is HTML. The interface to HTML is quite cumbersome. Although there is nothing preventing an ASP.NET programmer from programming like a PHP programmer (using MVC) the majority of projects make heavy use of well developed non-MVC controls. There is generally a longer learning curve with this technology, although for the most part, it pays off.

Again, if your project is complicated and your budget is limited, I would stick to ASP.NET. It's worth the extra cost to use a Windows server. If your project is simple, it really doesn't matter which you use. PHP will run a bit slower, but it will be more portable and can run on just about anything. If you need performance and portability, Mono may be an interesting choice, as long as you are willing to live with the limitations.