Neal Grosskopf Dot Com A NG Designs Site

Neal Grosskopf Dot Com Return To Neal Grosskopf's Home Page a NG Designs Site

Toolbar

  • Skip To
    • Menu
    • Content
  • Validate
    • HTML
    • CSS
    • WAI
  • Font Size
    • Small
    • Medium
    • Large
    • Extra Large
  • Account
    • Create Account
    • Login

Interact

Navigation

  • Home
  • Interact
    • Blog
    • Message Board
    • Open Links
    • Open Videos
    • Photo Gallery
    • Polls
    • Reviews
    • Geek Speak
  • Designs
    • View All Designs
    • Professional
    • Corporate
    • Templates
    • Personal
    • Club
    • NG Designs
    • Web Guides
  • Files
    • Artwork
    • Wallpapers
    • My File Downloads
  • Extras
    • Bookmarklets
    • The Magic 8-Ball
    • Career Builder RSS Feed Generator
    • Monster.com RSS Feed Generator

Converting A Site From Classic ASP To PHP: Some Things To Consider

  • Actions
    • View All
    • View Article Statistics
    • View User Statistics
    • Search
    • Subscribe to RSS Feed

Recent Articles:

  • Tracking Outgoing Links in Google Analytics Using Event Tracking and jQuery
  • Tracking Internal Website Campaigns With Google Analytics and PHP
  • Everything You Need to Know About jQuery's .data() Method
  • Google vs. China: How It Will Affect Web Workers
  • Don't Like The New Facebook Home Page? Fix It With 5 Lines of CSS
View All

Subscribe To Feed
loading Subscribers

I'm On Twitter loading

Aug 19

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?

Tags: asp, php ( Add Tag )

Bookmark This Page:
  • Digg loading
  • Delicious loading
  • Re-Tweet loading
  • StumbleUpon loading
  • DZone loading
  • Snipplr
  • Script & Style

Replies:

  • Chris Retlich
    Chris Retlich
    #1

    Wed. August 19, 2:46:52 PM

    Including functions in "footer" include files:

    My understanding is that ASP takes the source code, scans it for includes, finds those files and basically copy-pastes their contents into the parent document (in memory, of course), and does this recursively until all included files are merged together. Then it takes this conglomerated file and parses it as though it were one big file. PHP behaves differently, it reads and parses the parent document, and if it comes across a call to the include() or require() functions it then executes those, thus allowing you to conditionally include a file by wrapping it in if statements. Each method has its benefits and limitations.

  • Chris Retlich
    Chris Retlich
    #2

    Wed. August 19, 2:50:16 PM

    Oh, and include(), require(), include_once(), and require_once() are "language constructs", not built-in functions, but basically act just like a function with a few caveats.

  • Neal Grosskopf
    Neal Grosskopf
    #3

    Thu. August 20, 12:58:05 PM

    Chris, I'll let you 'slide' on calling include() a function. You are, afterall much smarter than I regarding PHP.
    You'll probably be suffering on a much larger scale than I did when you get around to converting the Lakeland website over to PHP soon.

  • Phillip Copley
    Phillip Copley
    #4

    Tue. August 25, 8:51:45 AM

    Can I ask what the point of the firstname/lastname functions are? I can't think of any example where you would hard-code a first name and have a function taking in the last name as a variable.

    I know it's probably a hypothetical scenario, but if you're pulling from a MySQL database you wouldn't need the $GLOBALS[] array so I'm just a little confused on that aspect of it.

    Thanks for the article. I sent it to my boss, who is a big ASP guy.

  • Neal Grosskopf
    Neal Grosskopf
    #5

    Tue. August 25, 3:09:45 PM

    @Phillip, you are correct. It's just a a hypothetical scenario (and one I randomly came up with while writing this). I probably should have just used the industry standard 'foo' and 'bar'.

  • Daniel Mitran
    Daniel Mitran
    #6

    Wed. August 26, 5:06:36 AM

    white screen of death = error reporting turned off.
    See here: php.net->error_reporting

  • Bilal Cinarli
    Bilal Cinarli
    #7

    Wed. August 26, 9:17:40 AM

    for the isset example
    you could use
    if($variable != "")
    {
    //do something
    }

    syntax

  • Henrik Bjorn
    Henrik Bjorn
    #8

    Thu. August 27, 8:58:24 AM

    So this is basically a post on sutff NOT to do when writing PHP.

  • Neal Grosskopf
    Neal Grosskopf
    #9

    Thu. August 27, 11:22:26 AM

    @Henrik No, just observations I found when converting my site over to PHP. I'm by no means a "perfect" web developer, my strongest qualities are in design and CSS.


Reply:

B U I Link Font Color Edit Preview ?
Characters: 0 Textbox Size: [+] [-]
   

  • New Comment
    #10

    Sat. July 31, 1:14:25 PM

Font Size:

  • A
  • A
  • A
  • A

Search:

Places:

  • Blog
  • Message Board
  • Chat Room
  • Geek Speak
  • Open Videos
  • Reviews
  • Tag Cloud
 

Site Links

Site

  • About
  • Contact Me
  • Site Map
  • Site Search

Interact

  • Blog
  • Geek Speak
  • Message Board
  • Open Links
  • Open Videos
  • Photo Gallery
  • Polls
  • Reviews

Random Stuff

  • Hosting by FatCow
  • © Copyright 2005 - 2010
  • Modified: Friday, July 16, 2010
  • Load Time: 0.3 Seconds

Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes