I’m sorry but why can’t php be more like C# with ASP.NET?

If you look at my earlier posts, you’ll see I’ve become a bit of a Mac fan but when it comes to programming I’m sorry but you can’t beat Microsoft. I’ve been developing personal sites (such as RetailHealing.com) using php for about 3 years now but in my ‘real’ job I use ASP.NET in C#. You may disagree but I think that gives me an unbiased perspective on the two environments.

Whatever people say about Microsoft, they do create very excellent programming environments. I find Visual Studio easy to work with once you understand the configuration, build and publish model and I’ve yet to find a php development environment that makes programming more straightforward with features such as debugging, dynamic type ahead/autocomplete - I have heard that zend studio is the thing to use for php but I’ve personally not tried it yet.

Now to C# and ASP…

OK, so C# is very much a clone of Java and PHP but that suits me because I was trained in C and the move to Java, PHP and C# took very little time. But what I really, really like about C# and ASP is the way is handles web page processing and particularly forms with the code behind model. PHP is crying out for something like this. The closest tool I’ve found for this is Smarty [www.smarty.net] but what I would really like someone to do is implement the following ASP/C# features in PHP:

Postback handling
Panels
Labels
Validation
Authentication

Postback handing

ASP and C# manage all web interaction with the code behind model. The HTML page (.ASPX) controls the layout while the C# page sits behind the html and control the functional logic. Each time you click a button or interact in some way with the page, ASP initiates a ‘postback’ (actually a web form submit) and kicks off a page load and/or method in the C# code.

Of course, php gurus will say that all ASP is doing is using standard web form processing with some Javascript to do this - and of course it is. But I don’t have to write it, it’s all hidden from me as the developer and I can concentrate on the core logic. This also means that the code behaves consistently.

Panels

ASP:Panel - a great feature that I use all the time. Build a web page consisting of a number of panels - for example, one ‘welcome panel’, a standard user panel, an administrator’s panel and perhaps an error panel. Now, in your code you can make each panel visible or invisible at any point in the flow of the application.

Again, in php you can do this using divs with css and hiding sections as needed but ASP needs way less code to do the same and you don’t end up with hidden divs on the page which Google can frown upon.

Labels

OK, php is similar here. Instead of the php <%=variable %> in ASP you use <ASP:label id=name>but that same label can have serverside code behind it, click events, validation and much more.

Validation

Form validation in ASP is excellent. Need a field to contain something before the user can submit the form? Simple. Just add some validation code to the field along with some helpful error text to guide the user.

Authentication

Finally, Microsoft hasvery advanced in-built authentication and the web.config file which as far as I know, php has no equal of. Tell web.config that you want forms validation using a login page hooked into a user database table and that’s pretty much all you need to do to have a secure website. On each page, just check that the user is authenticated (User.Identity.IsAuthenticated) and that’s it.

I’m probably going to get a lot of flame posts from serious PHP programmers telling me about why I’m wrong and that there are ways to deliver the same functionality in PHP and I look forward to hearing about them. I think there is an audience of PHP developers who really need these features.

PHP sessions and simplexml_load_string

In all the sites I’ve developed to date, it’s actually quite rare to find a php bug so although this issue took me a day to fix, I’m actually quite pleased to have found one.

The issue:

Basically, using the php simplexml_load_string() function appears to affect session handling and corrupts the data used in the $_SESSION data causing the error “node no longer exists” error when session_start() is called.

I was using the session system to keep a breadcrumb history for a store I’m developing (retailhealing.com) and had a handful of session varables, each representing a page in the breadcrumb history (crumb1, crumb2, crumb3, … etc).

The code was something like this:

session_start();
...
for ($i=1;$i < 5;$i++)    {
   $snode='crumb'.$i;
   $sdesc='crumbd'.$i;
   if (!isset($_SESSION[$snode])) {

 	$_SESSION[$snode]=0;
 	$_SESSION[$sdesc]='';
  }
}

...

[later in the code, I was also using simple XML]

$parsed_xml = simplexml_load_string($xml);

After the first call to session_start(), when the page was refreshed, I kept getting a ‘node no longer exists’ php error and I couldn’t figure out what was going on.

The solution

A Google search revealed a case similar to mine but the unfortunate poster seems to have been initially flamed by the respondents who rejected it as a bug.

This case was discussing the use of the simplexml_load_string() which I was also using in my code. As a test, I commented out all calls to this function and reloaded my pages. Everything worked just fine.

This pointed the finger of blame directly at the simplexml_load_string() after I of course had spent hours looking at how I was using the $_SESSION[] calls in my code looking for an elusive typo!

So, what was I to do? I could of course just not use simplexml and use a more complex XML parser but I really wanted a quick solution.

The answer was in fact included at the bottom of the case returned in my initial Google search. You need to cast all non string values assigned to session variables to strings as follows:

 $_SESSION[variable]=(String)value;

So, my code snippet above now looks something like this:

session_start();

...

for ($i=1;$i < 5;$i++)    {

   $snode='crumb'.$i;
   $sdesc='crumbd'.$i;

   if (!isset($_SESSION[$snode])) {

 	$_SESSION[$snode]=(String)0;
 	$_SESSION[$sdesc]='';
  }

}

This is the first and only time I’ve had to use casting in PHP and I’ve no idea why I need to do it in this case… I’m sure there must be someone out there who can explain?