Jump to content
MakeWebGames

Differences between PHP and C# / ASP.NET


a_bertrand

Recommended Posts

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.

Link to comment
Share on other sites

Very good post, and I mostly argee however I have a few points to note as well..

*) C# without aspx won't work at all, you need aspx to serve the webpages.

*) You are also comparing php versus a whole framework basically. php can do what aspx does. The thing is, .net framework is that much more so it can handle c# ajax components and what else, it's all bundled in one pretty nice framework.

*) PHP and sorts are in my eyes, stll easier to install, especially on linux machines

*) (proper- no drag & drop) learning curve all together is easier on the PHP side

*) .net side can become expensive, it doesn't have too though.

 

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.

You can do this in perfectly fine in PHP as well, even in one file if you wanted.

Also, remember that there are some good existing frameworks for PHP, like CodeIgniter, Zend, Symphony, ... which help a lot during development

Link to comment
Share on other sites

Sorry to say this, but you are basically bashing out on PHP. They are even remotely in the same league.

PHP is an interpretive language, and will not be able to do all the things C# can.

Let me just also give PHP some credit here....

PHP can run on ANY platform, C# can natively ONLY RUN ON WINDOWS driven platforms.

PHP can actually run background tasks, it just needs a clever programmer to do it. And besides, PHP was NOT designed for those sort of tasks.

PHP is SIMPLER to learn and use than C# will ever be.

I'm not trying to be rude, but you really cannot compare these two languages. It's like comparing RPG to Javascript, or apples to tomato sauce.

Linux is the popular variant on web servers, and thus why people like PHP so much. It's like C/Perl is the language for Linux. Just as is C# for Windows.

Link to comment
Share on other sites

Nickson:

- I choose to pick what both languages offer natively (that means without adding yet more packages to it). I must admit I have not much experience in the Zend framework nor in the other but for what I saw none are even 10% as good as the .NET one.

- C# without aspx CAN server webpages, but you must then handle the requests by yourself (I did some soft like that as well ;) )

- PHP is easy to install on linux... but depending on your linux package it is way not much harder. For example ubuntu offers all the MONO packages ready where you just have to choose them... Not hard.

- Drag / drop is normally easier than trying to figure out what to write... specially when you start. So no I don't think PHP is that much easier to learn.

- .NET is not expensive... as it can all run on free software (monodevelop works perfectly on windows too). PHP can be expensive too if you start with the Zend servers and all ;)

Yet PHP is not something which must be trashed at any costs. It offers still some easy to edit / modify where C# must be compiled. Even if compiling ensures you don't have syntax errors BEFORE trying the code. PHP offers also a lot of pre-made software free to use like wikimedia, forums, and all what you would need.

This post wasn't there to say PHP is dead, simply to show in what C# is superior and that it offers INDEED something different and interesting to learn.

Spudinski:

- C# is multi plateform, and can run virtually on all plateform as PHP. BTW it runs on iPhone and Android too ;)

- C# and PHP are 2 programming languages so yes you can compare then as well as compare their framework

- PHP CANNOT run background tasks. Running something with SYSTEM with an & to push it in background is not a solution as you can't control it anymore. Starting another process like a demon on linux and then have sockets link from PHP to communicate with it is not running something in background. Crons are also not background tasks. So if you have other tricks please explain them as I really don't see.

- PHP is simpler to learn? No, I really doubt it. You know maybe more PHP than C# then obviously you tend for PHP otherwise I shown to new apprentices (at work) PHP and C# and let them try to learn one or the other. I can tell you that they picked much more rapidly on C#.

- Am I bashing PHP? Yes partially... and for the purpose to show you guys that there is other technologies than the one you all seems to use.

For those which doesn't know, I'm a Zend certified PHP engineer, and I have developed a full MMORPG in PHP / JS (http://www.nowhere-else.org) so I do know VERY WELL PHP. And do not trash it. I still use it for some stuff. Simply for a lot of things C# is faster to develop with and give way better results in a shorter time. Sure you can get for most things the same results in PHP, but it would cost you a lot more efforts then why? Just because C# is evil? That's exactly what I want to demonstrate here, it's not and it offer some goodies.

Link to comment
Share on other sites

10 min to write it:

http://base.nowhere-else.org/temp/webcalc/Default.aspx

Default.aspx (the non compiled part):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebCalculator._Default" %>
<html>
<head runat="server">
   <title>WebCalculator</title>
</head>
<body>
   <form id="form1" runat="server">
   <table border=0>
   <tr><td>Value 1:</td>
   <td><asp:TextBox ID="value1" runat="server" Text="0" /></td>
   <td>Value 2:</td>
   <td><asp:TextBox ID="value2" runat="server" Text="0" /></td></tr>
   <tr><td colspan=4 align=center>
       <asp:Button ID="Button1" runat="server" Text="+" onclick="Oper_Click" />
       <asp:Button ID="Button2" runat="server" Text="-" onclick="Oper_Click" />
       <asp:Button ID="Button3" runat="server" Text="*" onclick="Oper_Click" />
       <asp:Button ID="Button4" runat="server" Text="/" onclick="Oper_Click" /></td></tr>
       <tr><td colspan=4 align=right><asp:Label ID="result" runat="server" Text="Enter 2 numbers and press 1 operation button." /></td></tr>
   </table>
   </form>
</body>
</html>

 

Default.aspx.cs (the compiled part):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebCalculator
{
   public partial class _Default : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {

       }

       protected void Oper_Click(object sender, EventArgs e)
       {
           Button btn = (Button)sender;
           string operation = btn.Text;

           double v1 = 0;
           double v2 = 0;
           if (!double.TryParse(value1.Text, out v1))
           {
               value1.Focus();
               result.Text = "Value 1 must be a number!";
               result.ForeColor = Color.Red;
               return;
           }
           if (!double.TryParse(value2.Text, out v2))
           {
               value2.Focus();
               result.Text = "Value 2 must be a number!";
               result.ForeColor = Color.Red;
               return;
           }
           result.ForeColor = Color.Black;
           switch (operation)
           {
               case "+":
                   result.Text = "" + (v1 + v2);
                   break;
               case "-":
                   result.Text = "" + (v1 - v2);
                   break;
               case "*":
                   result.Text = "" + (v1 * v2);
                   break;
               case "/":
                   if (v2 == 0)
                   {
                       value2.Focus();
                       result.Text = "Division by 0 is impossible...";
                       result.ForeColor = Color.Red;
                       return;
                   }
                   result.Text = "" + (v1 / v2);
                   break;
           }
       }
   }
}
Link to comment
Share on other sites

Here is the PHP version. Took me 20 min to do (with the same functionalities).

http://base.nowhere-else.org/temp/webcalc/calc.php

calc.php:

<html>
<head><title>PHP Web Calc</title></head></html>
<body>
<?php
if(!isset($_POST["value1"]))
   $_POST["value1"]="0";
if(!isset($_POST["value2"]))
   $_POST["value2"]="0";

if("".($_POST["value1"]+0) != $_POST["value1"])
   $message="<font color=#FF0000>Value 1 must be a number</font><script>document.forms[0].value1.focus();</script>";
else if("".($_POST["value2"]+0) != $_POST["value2"])
   $message="<font color=#FF0000>Value 2 must be a number</font><script>document.forms[0].value2.focus();</script>";    
else if(isset($_POST["oper"])) switch($_POST["oper"])
{
case "+":
   $message=($_POST["value1"]+0)+($_POST["value2"]+0);
   break;
case "-":
   $message=($_POST["value1"]+0)-($_POST["value2"]+0);
   break;
case "*":
   $message=($_POST["value1"]+0)*($_POST["value2"]+0);
   break;
case "/":
   if(($_POST["value2"]+0) == 0)
   {
       $message="<font color=#FF0000>Division by 0 is impossible...</font><script>document.forms[0].value2.focus();</script>";
   }
   else
       $message=($_POST["value1"]+0)/($_POST["value2"]+0);
   break;
default:
   break;    
}
else 
   $message="Enter 2 numbers and press 1 operation button.";

echo "<form action=calc.php method=post>";
echo "<table border=0>";
echo "<tr><td>Value 1</td><td><input type=text name=value1 value=\"".$_POST["value1"]."\"></td>";
echo "<td>Value 2</td><td><input type=text name=value2 value=\"".$_POST["value2"]."\"></td></tr>";
echo "<tr><td align=center colspan=4><input type=submit name=oper value=+>";
echo "<input type=submit name=oper value=->";
echo "<input type=submit name=oper value=*>";
echo "<input type=submit name=oper value=/></td></tr>";
echo "<tr><td colspan=4 align=right>$message</td></tr>";
echo "</table>";
echo "</form>";
?>
</body>
Link to comment
Share on other sites

So things to see:

- Both applications have the exact same functionalities.

- Both seems to work as fast.

- Yet, on the C# all the "html" has been created via drag drop (more or less beside some attributes to change by hand but yet always with code completion).

- In C# I wrote exactly only 43 lines of code... which is in my mind more readable than the PHP equivalent.

- In C# I didn't wrote any JS nor HTML inside the code... where in PHP I had to.

Link to comment
Share on other sites

C# is surely a good language, and it offers a lot just like PHP does. However php is "more open" than C# is. C# is backed by Microsoft where php doesn't have 1 single huge rich company behind it ... which clearly is a benefit.

Also, for the whole application in C#/aspx you used 86 lines versus 52 in the PHP ! You either count all of them or none of them. You say you had to write html/js in the code part, well you could have avoided that if you used an object or whatever if you just used <?= $var ?> and thus separating the basic code from the presentation, it's all on how you want to do it. And yes C# has benefits by being a top language that will be changed to byte code whereas php is straight to the bottom .. you can see that by the colouring of the text.

 

One last thing I really have to mention! Create both applications using a simple text editor with syntax highlighting. C#/aspx, and all languages that can be used via Visual Studio benefit So so sooo much of Visual Studio as it really is one of the best in existence to this day. If only php has an IDE up to par with Visual Studio, it would be so great, some like Komodo & PhpDesigner are coming close but are still not what it could be. Again, such a neat "trick" or business move by Microsoft to provide the whole framework with scuh and awesome IDE.

 

EDIT: C# looks cleaner indeed, but that's just the developer, I have seen ugly unreadable C# as well. it just takes a bit more effort in php.

Link to comment
Share on other sites

bluegman991: Flash is a pure browser client soft, where C# let you develop server, client and more. So sure we could compare Silverlight and Flash but is it what you want? And yes the goal of this thread was indeed to push some of you to try C# ;)

Nickson: true, the IDE does have his impact (even if I used Zend Studio for the PHP part). True, the total lines of number (between what I wrote and what not) in C# is higher, at least in this example. And yes C# is backed by Microsoft (even if it is an ECMA standard) where PHP is more driven by the community. I must admit you are right. However I still have a strong preference toward C# for some more complex application.

Link to comment
Share on other sites

Nickson asked for a memory and CPU usage by the 2. To do so I made 2 CONSOLE application (one PHP and one C#)

Both runs on the same machine on linux.

PHP Source:

<?php
$arr=array();
for($j=0;$j < 10000;$j++)
{
       for($i=0;$i < 10000;$i++)
               $arr[$i]=$i*2;
}
?>

 

C# Source:

       static void Main(string[] args)
       {
           int[] arr = new int[10000];
           for (int j = 0; j < 10000; j++)
           {
               for (int i = 0; i < arr.Length; i++)
                   arr[i] = i * 2;
           }
       }

 

As you see it's the same code.

Memory usage for C#: 88820 kB

Memory usage for PHP: 187872 kB

CPU usage for C#: 0m0.237s

CPU usage for PHP: 0m13.007s

Now the memory usage is... biased as PHP MUST load all the libraries in memory even if not used where C# loads what it needs beside the main VM.

On the CPU side... well it's without comments...

Link to comment
Share on other sites

Something I actually knew without doing a test ... but I would rather see how these test perform during a real "compatible" test. In other words, php served via apache and the output on a webpage, same with C# but then via aspx & iis ;). That's what I actually meant, not just a console test

Link to comment
Share on other sites

My only criticism of your overview is that it many of these items are not a comparison of C# to PHP, but rather the Web Forms framework to PHP. While WF does automate quite a bit of code generation, it often comes at a price in terms of speed, cleanliness, and request / response sizes (due to view and control states which can become quite large). .NET's modular composition allows almost of all that to be remedied by overriding base system objects, so it's not necessarily a deficiency - and if it were, it would be with WF and not C# itself.

Link to comment
Share on other sites

True I compared the ASP.NET framework (Web Forms) to PHP and not plain C#, but as said I want to compare what offers ASP.NET natively with what offers PHP natively.

It is true that ASP.NET includes a few things like the viewstate which is nothing else than the serialization of the properties of the controls which tends to be big, but in most cases it will not harm. Also if you don't want you can as well code without all those and just generate plain HTML out of your C# code.

Link to comment
Share on other sites

Another item of interest on the subject of ASP.NET would be the new-ish MVC framework which is distributed with .NET 3.x and 4.x. While you lose the ease of drag-and-drop development and the event driven nature of the Web Forms framework, it more than makes up for it with automated tests and clean code separation.

Link to comment
Share on other sites

  • 2 years later...
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.

Wow, really good and helpful! Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...