Leaving C# (for a while) to Try PHP

To fit in as a .NET/C# Dev I should never use the word PHP (except when cursing). But I want to learn, to know more so I decided to give PHP a try. There are numerous of big, wellknown applications written in PHP so it cannot be that bad, can it?

As a matter of fact, my blog is powered by WordPress which is written in PHP. From a user perspective I think that WordPress works fine. The basic functionality for editing and handling posts, comments and images works great. There are themes for customizations and plugins fore extensions. So I decided that it was time to dive in under the hood and learn more about WordPress and PHP.

In this post I will focus on PHP as a language and platform and skip over the WordPress details.

Basic Architectural Principle

PHP reminds me of ASP before ASP.NET came along. everything is driven from the html template. It’s like an ASP.NET Web Page (where the Razor engine drives the site, instead of starting in the controller). Additional functions can be defined in a separate php file (that WordPress somehow magically includes).

<?php if ( !is_single() && get_previous_posts_link() ): ?>
  <ul class="pager">
    <li class="previous"><?php next_posts_link('&larr; Older Posts'); ?></li>
    <li class="next"><?php previous_posts_link('Newer Posts &rarr;')?></li>
  </ul>
<?php endif;?>

One of the things that I miss in PHP is proper support for master pages. In WordPress themes this is solved by including a separate header template that contains the html header as well as the content that is in the top of each page. Similarly a footer template is used for the page footer. I’ve seen similar constructs in old ASP.NET solutions that were written before master pages appeared and I’ve never liked it. Having the opening and closing html tag in separate files makes it far too easy to get malformed HTML.

I can see that conceptually PHP is probably a lot easier to get started with than a full MVC framework. Just start with plain HTML and then add a few small PHP snippets where needed. Over time the PHP snippets grow and do more and more work, but there is a very straight path from static HTML into PHP while getting things done all the time.

ASP.NET and C# on the other hand requires that you learn the C# language first, including what a type system is. Then you have to get your head around the MVC concept which requires some closer-to-the-metal understanding of how HTTP really works. If I were to teach my kids how to make a simple web page with some logic behind it would probably be a faster start with PHP than with ASP.NET and C#.

The PHP Language

PHP is not a a language that has been designed; it has evolved.

The huge difference between PHP and C# is that PHP is a dynamic and interpreted. I miss the compiler. I miss the compiler errors. With PHP I just get a blank page when something goes wrong (There are probably better ways to set up the dev environment where proper errors are displayed). In C# I do get a detailed compiler error message or warning when I’m doing anything stupid.

Personally I very much prefer statically typed languages. Dynamic languages are great for getting some code running quickly and easily, but over time when the code base of the project grows the type system helps preserve the integrity of the complete system.

Another thing that feels awkward with PHP is the control structures. sometimes they use C-style if( ) { } but sometimes they use a more verbose syntax.

<?php if ( $a == 1 ) : ?>
  <span class="warning">The a variable is 1</span>.
<?php endif; ?>

It is used when the php block is ended to allow some plain HTML content before the control structure ends. Whenever I write that code I miss Razor deeply. Razor’s ability to seemlessly integrate C# flow of control structures and html by utilizing their differences in syntactical elements is clearly ahead of the PHP syntax.

PHP arrays is another source of confusion for me. They are created by a call to the function(?) array() and come in two flavours. Normal indexed arrays and associate arrays. An index array is the same as an array in most other languages. An associate array is much closer to a C# Dictionary or a JavaScript object. Confusing.

The Verdict

Don’t use PHP.

Right now, I can’t see a use case where PHP would be my first choice for building anything.

  • For enterprise web applications a mature statically typed environment such as ASP.NET MVC is to prefer.
  • For something that’s easy to get started with (such as helping my kids doing their first web site) I’d use ASP.NET Web pages.
  • For a major web application that must be built and run on open source platforms node.js or NancyFx on Mono looks like a better choice.

I’ll continue to fit in as a .NET/C# Dev and never use the word PHP (except when cursing). I’ve learnt, because I gave PHP a try. Even though there are numerous big, well known applications written in PHP it is as bad as people say.

Don’t use it.

34 comments

    1. Thanks, I think I’ve seen that post before and had it in mind when I wrote my post, but I couldn’t find it.

      It’s quite lengthy, but goes deeper into various details of the PHP language. After reading it, I’m even more convinced about my verdict to not use PHP.

  1. Yet again another article that vilifies PHP for the things it doesn’t force a developer to do. If you need a language to hold your hand and stop you making bad programming choices, then PHP is not for you. As a versatile and very powerful language (that permits MVC as well as not, in equal measure) then PHP works great. This whole article simply reads as someone simply preferring the devil they know.

    1. Thanks for your comment,

      Could you please elaborate on the things I’ve got wrong? I’m by no means a PHP expert so if I’m complaining because I’ve misunderstood something please enlighten me.

      I certainly don’t need a language to hold my hand. I’ve written code in x86 assembly, M68k assembly, C, C++, Ada, Lisp, Java, JavaScript, Basic, QuickBasic, Visual Basic, Delphi, SQL, TSQL, PL/SQL, Turbo Pascal, VHDL and even some thousand lines in an assembly language I designed myself for the CPU that I designed myself with an instruction set that I had made myself (I might have forgotten a language or two on the list).

      What I’m looking for in a language is if it has a certain style in how it is used. Once you get to know the style of the language, new features of the language should be quite self evident on how to use. I’m sorry, but I don’t see that style in PHP. After having read the link that Jonas Gauffin posted I’m even more convinced that PHP is hard to use. Please understand me right, I have no problems with writing code in with tools that are hard to use when needed. Web development isn’t such a case. For web development I want to be able to focus on the functionality of the system I’m developing, rather than having to occupy a big part of my limited mental capacity on strange exceptions in the language I’m using.

      1. Yet another defensive commenter that doesn’t bother to come back and actually explain any of the accusations he made. I thought your post was great. I’m a node developer that used to work extensively in ASP.NET MVC, and at my new job there is a fair amount of PHP intermixed with node. It was nice to hear what you thought since you came from a similar background.

      2. Hi there!

        I’m a PHP Developer but I also like C# a lot. I agree with Lewis Walsh reply in some part.

        PHP makes you look at things and try to do your best. Find some code standards for example, see what’s better performing, etc.. In your example you complained about the verbose stuff. For me it’s one of the best things of PHP. On the view file you shouldn’t have too much logic. You should have something like:

        ….
        isAthenticated) : ?>
        Login here!.

        If you would do this the worst and other way:

        ….
        isAthenticated) {
        echo ‘Login here!’.
        }
        ?>

        Other then this you also have template languages like Twig (which I think C# also has).

        For me it’s not really about being better or not, but rather which language is appropriate in terms of costs, available developers,other resources, etc.. Then you can also rely on other technologies that along with PHP, give you the opportunity to make great apps. Facebook uses McRouter, HHVM, etc.. My company uses Solr, RabbitMQ, and other stuff. Just as an example.

        In my previous company two guys left to work on a C# project and they really liked it. However their opinion was that sometimes, certain stuff would give them more work. Other stuff they had less work. But overall they really liked C# and keep working on it.

        Another point for PHP is the current available frameworks. There are some really nice frameworks to choose from.

  2. These posts are always kind of interesting — in a nutshell it’s kind of like “I’ve spent many years becoming an expert at what I know, so now I’m going to look at something else for a few hours and generate an opinion on it without gaining a full working knowledge, and then assume that it’s no good”. I don’t intend for that to be snarkey, I can’t really blame you — we all kind of do this with what we know vs. what we don’t (me included).

    Just a few notes:

    “everything is driven from the html template” — as a php person, I’m not sure what this means. There aren’t html templates as part of php — everything is done via a php file, or even better, using a framework, which may use a php file to control program flow. I actually still use an old framework, which uses xml files as the controller (but I’m sure there are better setups these days). From there, you can break the site into individual php files and modulize it as much as you want.

    “One of the things that I miss in PHP is proper support for master pages.” — I just use a framework that allows you to create master pages and have content inserted into areas of the page. WordPress templating actually isn’t that great in my opinion (where it separates the top and bottom of a file), but this is more of an issue with wordpress and not php. With the framework I use (and others that are out there), you can insert multiple layers of pages within each other, so you can have a basic exterior template for your site, and then have various other templates that you can insert into that template, etc… I think php is more open ended than c#/.net, so you have to find the best solution for your need. It might take a little more work initially to find what works best, but I think the openness is a strength of the language.

    “I miss the compiler errors.” — Just turn on error reporting and it will tell you what you did wrong. I actually don’t like having to compile — it’s just an extra step. PHP will tell you what’s wrong without needing to compile.

    “Another thing that feels awkward with PHP is the control structures.” — So use the one you want and stick with it — I didn’t even know you could use an if statement how you’ve shown in your example (it’s always if(){} for me). You are right though in that I like Razor’s ability to pick out HTML from the C#, but not that big of a deal for me since any php code on front-end files is pretty limited.

    “PHP arrays is another source of confusion for me. They are created by a call to the function(?)” — You can go with $arrayitem[0]=”foo”, $arrayitem[1]=”bar”, or $array = array(“foo”, “bar”), and a few other ways; Associative arrays just allow you to use non-numeric keys — use them if you want, or don’t.

    1. Thanks for commenting,

      You are certainly right that I look at PHP from a C# perspective and also that my knowledge of PHP is limited. What I tried to do here was to take things one step further than most C# devs bashing PHP: I actually made something real in it (the theme the blog is currently running on).

      What I mean with “everything is driven from the html template” is that the php file (which I call html template as it can contain embedded html) is what drives the application. Compare that to an MVC framework such as ASP.NEt MVC where the the code enters a C# function first, which then hands off control (and data) to the view for rendering HTML.

      Anyways, thanks a lot for your comments. You are certainly right that my PHP experience is much more limited than my /.NET C# experience and my view is of course biased by that.

      1. tbh this post comes off naïve. I’ve programmed in both C# and PHP and you can set up your application to start with PHP then go to html templates exactly the same way C# does. Most big applications do it this way. You can even make html templates without any PHP at all and use a complete different templating engine like Mustache. You can do it yourself or you can use a framework like Laravel to do it for you.

  3. I really liked your post and can relate except I went the other way (from php to asp c#)
    Php has evolved so much that it has a bunch of parallel techniques such as the two ways to write the if statement. These are things that have been picked up along the way.
    You are also right in noticing the similarity with old asp; at one point you could convert asp to php by replacing asp tags with php tags…this was a one way process as you couldn’t necessarily go from php to asp.
    I want to point out that wordpress is not a good point of reference for php (not to discredit wp in any way I’m a proud user) but wp has a lot of legacy stuff that’s there for platform compatibility and might look messy to someone who just got around to taking a look under the hood.
    I think the biggest opportunity for php is getting a good IDE. I have a good configuration through net beans but you have to setup several plugins and still not come remotely close to what VS does and it’s not about holding hands as someone pointed out. If you program the same applications over and over, any language will do but when you need to figure out new stuff I’m all in for an IDE that will save you time and money before going into production such as VS.

  4. It is not a fair comparison. I think if you compare PHP and C# it is similar to compare orange and apple or you gonna compare French or Spanish.each one has their own applicability . but as a matter of fact PHP is lower level language than C# and you have to write code for smallest bit of your meant . that is , you don’t have any any tools which are available in C# such as grides , menus ,…. . But i believe that learning PHP is easier than C# .

  5. I don’t agree that it’s a comparison between C# and PHP, it’s nothing of the sort. Writing a C# app in Visual Studio for example, is not the same as building web pages in Microsoft Visual Studio.
    I agree that C# is probably easier to put functionality together, of course it is.

    But, what I like about PHP is the lack of hassle I get from removing all the glitchy Microsoft stuff from under my feet. No conflicting libraries, no side by side errors, incompatibilities between 32 bit and 64 bit Office (we are removing all our excel/DBF stuff as a result of that bug as we are tired of having to support Windows machines that have broken Office libraries). I especially hate the way it tries to make out like it writes code for you and you never have to touch it, in the end you end up hacking away like you would with notepad.txt using a raw language like PHP.

    I have about 18 years experience with Microsoft products (and currently Microsoft Visual Studio 2010) and they sell as they cause more bother than it’s worth. You think you’re saving time, but you’re not when you calculate up all the little problems and glitches you get, and that’s even if the problems are your own fault, that is part of it all.

    PHP at least only needs notepad, and a web server and that’s about it. In conjunction with Smarty templating everything becomes transparent and friendly.
    I also have a fair amount of experience with WordPress, CodeIgniter and CakePHP (ugh), and I’d say WordPress is a bad place to start, I’d say use CodeIgniter to get started with PHP and judge it from that with it’s delightful active record queries etc.

    Transparency!

  6. Hey Anders,

    Interesting to read your post and it’s good that you at least tried PHP. I think though that you perhaps either didn’t get enough material about what PHP is compared to ASP.NET MVC.

    You see, PHP is just a language. It is most certainly not a framework so any comparison is invalid — its apples and oranges. Unlike ASP.NET MVC in PHP frameworks come separately. These frameworks provide the rigid structure that I think you are craving here (see Laravel or Symfony 2). I’m afraid I can’t help you with the compiler errors but I think you weren’t using a decent IDE (PHPStorm is fantastic) and having errors switched off surely isn’t helpful to the debugging process. With .NET 5 (vNext) Microsoft is actually moving away from the compilation step so you may start to dislike that too!

    I have both experience with C#.NET and PHP and I must say that I do find the Windows environment the greatest challenge when managing the operations side of thing (IIS is a pain to manage compared to Apache for reasons I’m sure I’ll blog about one day!)

    I think you’ve fallen into the trap that a lot of people get into when they first start using PHP. It’s so easy to get started but it’s even easier to make a mess of things and not really understand how to use it. C# is exactly the same with the exception is that you are usually using a framework like ASP.NET to enforce a structure for you.

    If you would like a more guided introduction to PHP and the most common frameworks I’d be happy to help and perhaps you would have more of a positive and somewhat balanced impression on it.

    1. Thank you Kevin.

      Also I will add that the conclusion is too limited with only the cons not the pros :
      – PHP is free
      – PHP works on all platforms (Linux, Mac, Windows), on all servers (Apache, Nginx, IIS)
      – PHP has a big community, nice documentation (not the Microsoft incomprehensible one)

  7. I think you kinda wrong about c# itself. Both had the same problem upon object oriented design. PHP more flexible upon array rather casting List.
    What i saw on .Net developer confusion upon HTML and Server side scripting.
    Asp.net Webform is great and i have nearly transfer my php oop to C#, There’s a lot of limitation on c#.
    C# kinda i see movin like PHP .The only diffirent is just $ and var.
    E.g var dataReader = new SqlDataReader();
    or $dataReader = new SqlDataReader()
    PHP IDE kinda mess with java IDE I hate it and it make me slow down. Compare to Visual Studio .It totally diffirent world. If i wrote code in php,i do prefer notepad++ instead of bulky java IDE.My laptop allready max ssd drive and 8 gb ram still hang and slow upon eclipse/other even off all those plugin.
    My conclusion, both language are great PHP for fast quick code and C# for enterprise application.Enterprise mean i easily said to customer 1 language for mobile ,for excel plugin and system itself and i don’t want to waste time on uncertain mysql license issue or the more weird RFP php become..

  8. I’m a self taught php developer. I decided to start with php because it is much easier to understand and is very very flexible. I’ve attempted to learn c and asp.net but both are a pain to get started with. If prefer to learn on something that has no “pay microsoft” to be able to do this attached to it. if you are starting in web development, php and mysql are the way to get in the game

  9. I’ve stumbled upon this article by mere coincidence and I am literally stunned by such low-quality assessment made by a seasoned developer.

    I’m a developer who deals with distributed services so I use a myriad of languages on weekly basis and the list includes C, C#, Haskell, JavaScript and PHP.

    I like all of them, and usually when taking a look at a language that catches my eye – I do try to objectively analyse it. This article does a lot of harm. Personal preferences are good, and everyone is allowed to love or hate a language. But, bashing something by providing deliberately wrong insight is just destructive and bad.

    From what I can see, PHP you dealt with is tied to WordPress. That’s one of the worst products that ever came out, together with things like Drupal or Typo3 which are popular but honestly – those projects are terribly coded. I am sure you can find such bad projects in any language.
    That however, cannot be something you will use to judge a language.

    Do you judge a hammer by the state of the house built with it? I guess you don’t, no one would because that would be stupid.

    Sadly, the problem with PHP is that you can set everything up and that it works in many environments. That requires the developer to spend time configuring it – and no one does that. This is why you get a “blank” page. For example, I’m able to examine stack traces, frequency of method calls, errors based on levels and what not – it took some time to set it up, but I don’t get “blank” pages. This also means you’re running a web server that has its own configuration options (hence no message is displayed, but a blank page). This is not the fault of the language, as you hinted – the fault lies in you and your web server.

    You also mentioned an array – no, an array() is not a function call, it’s a construct – much like if() is not a function but a construct. Arrays are one of the best features of PHP because of its simplicity. In C# you need to use a Dictionary or a List (which is just fine) but with PHP you use an array to perform the task of both of them, interchangeably if needed.

    I don’t think you are qualified to correctly perform a language overview, unless it’s (as you haven’t written) personal and opinion based. You need to realize that a lot of people might read your article and arrive at incorrect conclusions. Also, given the fact you were so sloppy in this analysis, I must say that I do wonder whether you’re so sloppy when dealing with algorithmic problems in language(s) that you do like.

      1. Hi,

        What language and framework do you recommend to learn web development?

  10. This kind of posts is what makes me sick, simply because it is misleading.

    I honestly think that you should learn php and use it in a real life project before making any post like this.
    What you said about the master page, html and the lack of MVC is totally wrong, i really advice you to check php frameworks like, laravel, symfony, zend … and then update your article.

    Maybe if this article was wrote 10 years ago it will be acceptable, but in 2014 ?

  11. I tend to agree with the author, I have very limited PHP experience, and what little I have makes my skin crawl! (Even the language syntax is NOT logical (method in an object use -> and not “.” ). I have architected applications in ColdFusion (I miss this), .NET C# ASP.MVC, WPF & XAML, Flex & Action Script (way better than WPF), etc…

    So I have a question for the community.

    Today! You are the lead/CTO/CIO if you were to build an enterprise web application NOT a website (for internal use or resale dealing with say in the field of Finance, Healthcare, Procurement, Inventory, etc.., Integrations to AD/SSO ) and you wanted to build a team of developers (Web + Client Server ( if needed) ) what language would you choose.

    .NET + C# OR php

    1. You are right. if you ask me, I’d give you a list of things I need to have from Servers to Visual studio licenses to write and support the system in .Net, and I’ll get a team of C# developers, they all code under the same standards (one framework), php has tons.
      So .Net definitely.

      Unless you tell me to f…ck off with the bill I gave you. Then we’d go for an open source probably LAMP stack.

  12. Thank you for sharing your thoughts. And you’re right in general that C# is, by far, better language than php, it always will be (has multi-billion company behind it). The only thing I don’t understand is how you compared ASP.net – the framework, full blown framework vs php – the language. if you’d really like to see the difference between asp.net and php based framework. I’d recommend checking Symfony or Laravel against MVC + Web Api project.
    I again agree that c# kicks php’s ass, but please keep in mind that new web developers will be taking their first steps on choosing the right technology by reading posts like this one, let’s try to be fair here.

  13. I have been using both languages for 11 years. of course new developers will like .Net, it has easier environment to learn and quicker to develop. But i do not agree to compare MVC against a language. what the hell. Php is good, even better than c#. but Php MVC frameworks are hell compare to ASP MVC. But in the 3 years to come, laravel is going to be the best. For me the best MVC now ,is rubby on rail.

    On the other hand look at C#, you need windows server licenses, language is self license, mSsql license, and windows applications requires a big memory, you will need an expensive server to host your application. but with Php you will host on linux, which is free, you can you use PostgreSQL + laravel , try this.
    I am developing online betting system, i wanted to do it in C#, i showed the cost of hardware and windows licenses to my client he almost fainted, he told me please find another way to reduce the cost. Now i am using laravel and postgresql, i am really enjoying coding, Anders Abel should try this becore he compares PHP and C#.

    1. The things you said about C#, shows how clueless you are.

      C# requires no Windows Server license, you can actually execute C# on Linux through Mono and today through the power of .NET Core you can run C# on Linux, Mac and Windows and on mobile through Xamarin and guess what? everything is open source!

      You don’t need expensive server you can host ASP.NET on Apache through Mono.

      C#, specifically the Entity Framework (ORM) supports PostgreSQL and MySQL and more.

      You wish for PHP to be better than C#, really, you wish! it’s one of the worst languages to exist, show me something that you can do in PHP that the solution in C# isn’t multiple times better…

  14. Weird. You are comparing PHP with ASP.NET MVC which is a framework where in PHP, you have frameworks such as Laravel , Symfony etc. You should compare framework vs framework. This article is really misleading.

    1. This is an excellent comment. PHP has the flexibility to be used as procedural or OOP or through a framework. Using a MVC framework such as Laravel and utilising Composer for dependancy management is a completely different experience to writing basic PHP. Laravel also offers very helpful error messages.

  15. PHP and ASP.NET cannot be compared, PHP is a language while ASP is a framework, I agree with pro C# programmers, C# is most recommended when it comes to framework because they uses standard mvc (ASP.NET) unlike in PHP a lot of framework(Laravel, Codeigniter, Symfony, CakePHP, etc.) If you want to work as a team you need to choose what framework they use so that you standardize your codes. , but if you compare PHP and C# they are almost the same. one thing I hate in PHP is multi-threading, now a days multi-threading now supports PHP and thats a great news. credits to pthreads. :) now I can do anything on PHP. I made a tons of project like SAP S/4HANA made of PHP, Inventory Systems, POS, etc.

    1. PHP and ASP.NET can definitely be compared, PHP is a language but it is also a platform, that’s where the confusion starts.

      PHP has frameworks that work on top of PHP the platform just like ASP.NET has MVC or WebAPI.

  16. I’m far from calling myself a PHP developer, in fact I dislike everything about PHP but you mixed multiple things and by that I mean the following things:

    * PHP the language that is completely independent from its platform, you can write your own PHP compiler that emits native code.

    * You made the comparison in the wrong context instead of comparing languages you compared multiple things and then somehow mash things to fit your own point of view.

    Anyway, if you write about something, you really need to try and be objective about it, you really need to try it and double check that you get everything about it correctly; otherwise, you’re being unfair.

  17. After reading this article, I realized that the author of this article doesn’t really know how to work with PHP. We can’t blame him because he use to love C#. Well they are really different. If you will be staying in .NET you should use C# and if you want a cross compatible applications PHP would be useful. BTW, its not really the language that makes sense, its the developer that uses them. For me all programming languages is great :)

  18. This post is none sense you are comparing a poorly type cms as WordPress to the Microsoft Champion. PHP to have Champions. Zen, Symfoy, Laravel. Go check them before kicking on PHP.

  19. It’s a pity that you’ve tried to work with PHP like we were doing 15 years ago – without proper IDE with debugger, without good framework with templating engine, etc.

    However it is really strange that after such 5 minute experience you think that you have enough information to share your opinion :o)

    Please work with:
    – PhpStorm + xDebug
    – Symfony 4 (framework) + twig (templating engine)

    And do it at least for a couple of weeks (so you know them a little and can use them properly).
    And then you’ll find that PHP looks completely different.
    Because code for me it is a shame to write PHP code the way you did in this article. Good PHP developers don’t do it this way for more then 10 years.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.