
Originally Posted by
a_bertrand
Some of you may think C# / ASP.NET is yet just another way to develop web pages or do not even know that C# allows to develop web pages. With this little post I want to show some of the main differences between PHP and C# web development.
PHP Logic:
PHP generates directly the HTML / JS / CSS used to compose the page and produce the end HTML page directly from the code or if you are smart enough using things like templates engines. You must know HTML at least to be able to develop PHP web sites.
C# Logic:
You compose your page out of "web components" which are aware of their state and can fire events like "button click". Once a event is fired, your C# logic (no HTML / JS here) will change the state of the page components or add new one on the fly. Components are then invoked by the framework to render themselves. You don't have to know anything to HTML to be able to code.
A PHP register form:
You generate the HTML for the form, then somebody press the submit which call the same or another PHP file, the new file check the form data and either call again the first page (with hopefully some sort of errors) if it doesn't work, or save the data and goes on. In most cases the form data are lost if there is an error as it's annoying to put back the submitted data to the form and developers are lazy.
A C# register form:
Beside there is a pre-made register / login form, you could use the standard textbox, labels C# offers you. Then an ASPx button will be used to submit the form. On the event of the button click you check if all is fine, if not you simply show some text in a label, and all the form fields will keep their data without any work on your side. If all is fine, you save the data in the DB and redirect to a new page.
PHP templates
By default PHP doesn't make any separation between the look and the logic. So either you use additional packages or write yourself some sort of template engine. If not then you will have an hard time to change the look (beside changing CSS files).
C# templates
The logic code is separated from the presentation code. The presentation code is nearly a pure HTML code with some additional tags for the controls. The logic on the other side is a pure cs file (C# file) and is compiled to produce a .dll file.
PHP software distribution
When you give away a soft written in PHP you normally give out your sources as nothing is compiled. To avoid that you must use some special software to encrypt your code, and those soft are not free nor really safe.
C# software distribution
As the logic is normally compiled you can safely distribute your software. Yet of course there is some softs to de-compile C# code, in case you want to be safe also against that, you can also use some obfuscation software (not really expensive).
PHP performances
PHP is an interpreted language. Sure you can use some accelerators but still it remains a slow interpreted language. For most web pages it is fine, but as soon as you want to produce images on the fly or yet do some more complex operations PHP will start to use a lot of CPU and time.
C# performances
Performances varies between Linux and Windows machines. On Linux the performances are still like 10x the one of a PHP equivalent, but not yet really fast. On windows C# is nearly as fast as pure C++ code.
Background tasks on PHP
Basically impossible to handle... As soon as the web page is shown your program dies.
Background tasks on C#
You can have as many background threads and even keep things in memory. Threads will continue to run in background even after a page is loaded. You can cache things in memory for example from a database or you can even communicate between different web sessions for example to create a chat (all that is not directly possible in PHP).
PHP Ajax
PHP doesn't have any native support for AJAX. Either you code it yourself or try to find some library which help you.
C# Ajax
C# does have a full AJAX support. Which means you can have events which call back the server without reloading the page, you can have timers to pool data at regular interval and much more. You can even design AJAX aware components but any web components can be updated without any page reload.
PHP minimal tools
A web server with PHP support and some text editor.
C# minimal tools
A web server with ASPx support (apache with mod_mono or IIS), some text editor, and a C# compiler. However you can download and use MonoDevelop (free and open source) or use Visual Studio (much better but beside a free Express Edition it costs).
Debugging a PHP application
Quick answer: a nightmare. You need to put echo every 2 lines or if you are really skilled you can try some PHP debugger (which honestly I always failed to use correctly).
Debugging a C# application
Easy: inside Visual Studio (as well as MonoDevelop) you have a full debugger at your fingers. You can put breakpoints, let your soft run, and see how it goes with the variable and you may even change the value of a variable on the fly or move the "running step" where you want.
When would I use PHP
I would use PHP if I want a minimal Linux installation and yet do not develop too complex websites. Or if I need to be hosted on some host providers which doesn't offer any alternative for ASPx
When would I use C#
I would use C# for all web development which involve more than some simple dynamic web page. Visual Studio makes me gain a lot of time and the framework is really powerful.
Bookmarks