Well, after a month of spending my evenings coding till the wee hours of the night, I've finished converting my website NealGrosskopf.com from Classic ASP to PHP. After going through this grueling ordeal I thought I'd write up a quick article detailing my experience and perhaps save a fellow web developer some time should they ever endeavor this process.

ASP to PHP

Learn The Basics

If you're not familiar with PHP like I was, I'd suggest you learn the basic syntax differences between ASP and PHP. I pretty much self taught myself PHP using the tutorial website Tizag. I feel like I'm a pretty competent ASP coder so learning the basics of PHP wasn't too hard for me. Here's a few observations I found.

  1. Built In Functions

    PHP seems to like ordering parameters in built in functions slightly different than ASP. For instance in ASP you have:

    replace("Input String","Find","Replace With")

    In PHP the order is slightly different as well as having a slightly different function name:

    str_replace("Find","Replace With","Input String")

    Some other examples of this in PHP are str_len (len in ASP), strpos (instr in ASP) and explode (split in ASP)

  2. Parse Errors

    Forgetting to add a semi-colon ';' at the end of your PHP line = 'white screen of death'

    At least with my host's configuration (or possibly my own stupidity) parsing errors such as forgetting the semi-colon results in a very not-useful white, blank, empty PHP error page. This can make troubleshooting the error very, very hard.

    Fortunately I found a solution to this. Portable XAMPP. I essentially ran web server locally which allowed me to test out my PHP pages (minus the My_SQL database). For whatever reason, my local copy did not give me the white screen of death and let me see the exact error message. This helped me find the error 10x faster. I suggest if you're starting out with PHP, download Portable XAMMP and tinker with it locally, for free!

  3. Code Order

    One problem that I ran into was the placement of my functions within my code. In ASP I could include files at the end of my page, and even have functions within these includes be called even before the code has reached that area of the script. I suspect ASP pre-compiles the functions ahead of time while PHP does not. My solution to this was to re-organize my code so that all the functions appeared first within my include structure.

  4. Variable Scope, and isset()

    Scope Another problem I ran into was variable scope. In Classic ASP I'd often something like this:

    FirstName = "Neal" ... somefunction("Grosskopf") function somefunction(LastName) response.write FirstName & " " & LastName end function

    In PHP this will not work unless you use the $GLOBALS[] array to your variable in the function like so:

    $FirstName = "Neal"; ... somefunction("Grosskopf"); function somefunction($LastName) { $GLOBALS["FirstName"]; echo $FirstName . " " . $LastName; }

    I'd also change global variables inside functions in Classic ASP. To fix this in PHP simply add the global keyword to the variable:

    function somefunction() { global $FirstName"; $FirstName = "Some Other Name"; }

    I'm sure these aren't good programming practices, but hey it works!

    isset() In ASP you could do the following even if the variable has yet to be set:

    if variable <> "" then 'do something end if

    In PHP this is not acceptable. Instead you'll need to use a built in function called isset(). This basically checks to see if the variable has been set yet:

    if(isset($variable)) { //do something }

    This isn't a huge difference, but since I was using the ASP method quite a bit, I needed to re-learn the new way to accomplish this in PHP.

  5. Redirects

    Another common problem you'll run into when converting from ASP to PHP is redirects. In Classic ASP you could do a redirect using the following:

    response.redirect("thank-you-page.asp")

    In PHP if you need to use the header function

    header("Location: thank-you-page.php");

    Another thing to consider is if you place your header function further down your page, possibly after you've already output content to your page. If this happens then you'll need to use the output buffer, at the top of your page:

    ob_start(); I'd also recommend saving your session data before you do a redirect in PHP like so: session_write_close();
    Here's all the code at once:
    ob_start(); //Some other code here session_write_close(); header("Location: thank-you-page.php");

Redirect Those Old ASP Pages To PHP

The great thing about switching to PHP is you now have the ability to use the much hailed .htaccess file. My very limited perception of this file is it acts as a gatekeeper between every file on your website and the end-user requesting the files. It allows you to intercept any request made to the server. One sweet rule I found for .htaccess is the ability to redirect my old ASP pages to their PHP equivalent:

RedirectMatch 301 /(.+)asp$ /$1php

Its sad but that one line takes care of it all for me. Google will now crawl my new PHP pages, and users with bookmarks to the old pages will still find them. It's almost too easy!

Final Thoughts

Those are just a few of the differences I've experienced between PHP and ASP. I'm sure there's thousands more. Overall I'm very happy with my new PHP website and I'm happy I've finally taken the time to switch.

Anyone else convert an ASP site to PHP before? How was your experience?