ASP to PHP

Hi, my name is Lucas Chan. I’m also a resident coder at SitePoint HQ and I’ll be popping my head in here from time to time to help out with the PHP blog.

Over the past week or so I’ve been assisting a friend of mine redevelop one of his ASP sites in PHP (with MySQL). He knows ASP quite well but has never touched PHP before.

Having had bad experiences with language conversion tools I was reluctant to recommend one to him. I instead pointed him to the PHP manual and the wonderful archive of information at SitePoint.com. Harry also mentioned a syntax cross reference some time last year.

I thought it would be interesting to document some of the help I’ve given him over the past few weeks. Here

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • webmaster

    Personally I found converting from ASP to PHP a not a good choice… but thanks anyway.

  • Matthew

    TINYINT is fine for emulating boolean fields in MySQL, but better is the ENUM. It uses less space and can be configured for a specific number of values.

  • Ned Collyer

    “Incrementing a number is easy using the ++ expression.” and “Use ‘%’ instead of ‘mod’ to find the remainder of a division: $remainder = 5 % 4;”
    = the same as many other languages (including JScript which can be used for ASP).

    This article targetted at VBScript ASP conversion.

    I haven’t used PHP for a while so bear with me :)

    Please tell me the behavior of $i = $i++ is predictable and logical whereby the right of the equals is processed first.

    $i = 0
    $i = $i++
    $i is now 0

    $i = 0
    $i = ++$i
    $i is now 1

    If this isn’t the case, I’m not touching PHP ever again :)

    A thing to note I guess would be
    — is great for decrementing numbers.

  • Nico Edtinger

    $i = $i++;

    is the same as

    $temp = $i;
    $i++;
    $i = $temp;

    Thus it’s a complex NOP =)

    b4n

  • Ned Collyer

    er.. my comment above has lost some carriage returns

    $i = 0 < -- carriage return -->
    $i = $i++ < -- carriage return -->
    $i is now 0 < -- carriage return -->

    $i = 0 < -- carriage return -->
    $i = ++$i < -- carriage return -->
    $i is now 1 < -- carriage return -->

  • http://www.lowter.com charmedlover

    That is the case, but it is typical of C++ too. There is some reason behind it, but it isn’t coming to mind.

    I wouldn’t waste my time with ASP when PHP is tons better :)

  • http://www.sigma3w.com CodeLes

    [QUOTE=charmedlover]I wouldn’t waste my time with ASP when PHP is tons better :)[/CODE]
    A little biased there… ;-)

  • http://blog.casey-sweat.us/ sweatje

    I used on of those converters (asp2php I think?) for my first script. It got me 90% of the way there and I was able to tweak it and get the application running. From there, I started coding in PHP from scratch, having the converted script as a starting point. After I got used to PHP, I went back and re-wrote the converted script entirely, as I realized how bad it really was :)

    HTH

  • http://www.sipofwater.com/ fumbler

    i went the opposite direction attempting to use a php2asp and ended up essentially starting from scratch as it was way easier then trying to fix the code the script generated using the php2asp code as a reference as i re-wrote everything.

    i was quite happy going from asp to php and looking back now, find working in asp (with vbscript) to be a bit ‘archaic’ esp now that im going from php to asp.net with that same site.

  • http://lucaschan.com/ Lucas Chan

    [quote=matthew]TINYINT is fine for emulating boolean fields in MySQL, but better is the ENUM.[/quote]ENUM can be used for emulating booleans, I’m not sure this is the best method though.

    In fact, as of MySQL 4.1.0 BOOLEAN was added as an alias of TINYINT.

  • http://www.napathon.net/ vinyl-junkie

    I’ve rewritten applications from ASP to PHP, and the code itself was the difference between night and day. Obviously, there are some similiarities between the two, but given the fact that I made my applications object oriented as part of the conversion process, my PHP code bears little resemblance to its ASP counterpart. I didn’t find an easy way of making the code conversion; I don’t think there is one. However, with the help of SitePoint’s PHP books, the code conversion was fairly painless.

    One of the big hurdles in making the conversion from ASP to PHP that you failed to mention, which is equally as important, is the conversion of your MS Access database to MySQL. I’ve found a number of tools that are useful in this effort.

    Access-to-MySQL (Acc2MySQL), by Intelligent Converters, is the best tool in my opinion, although it’s not freeware. It allows you to create a dump file which you can then use to populate your database, or if your web host allows remote access to the server, Acc2MySQL provides a means of converting your Access database on your PC and uploading your MySQL database directly to your Web server.

    Using a free PHP application called CSV Importer, you can export your MS Access tables to CSV files, upload them to your web server, then run this script to do the database conversion. This application has a very easy user interface and seems to works well with any size MS Access database.

    ZenWerx DB Converter is also a very nice database conversion tool that provides all the same functionality as Acc2MySQL,and best of all, it’s free.

    This is by no means an exhaustive list of available database conversion tools, but hopefully someone may find these useful.

  • pryce

    Of course an Access-to-MySql converter comes in quite handy, but IMHO as far as the sheer asp-to-php conversion goes, the time spend on doing it by hand is well spend. Mostly one has to do manual tweaks anyway, often a painfully frustrating task, since most auto-generated code is structured otherwise than you would do yourself. And, more importantly, one learns quite a lot about php and its developer-friendliness compared to other languages when converting functional tasks from f.ex. asp to php. When ever you think “Gee, I wish there was a build-in function taking care of this…”, coming from most other languages, you have to get used to that, well actually there might *be* just a build-in function taking care of that for you. That’s only learned by doing, not by using any code-converter or generator.

  • http://www.intereactive.net TheTank

    Databse type should play no role in this discussion. You can use any type of database on ASP (IIS server) provided the database engine has an ODBC. All of my ASP sites are run on MySQL and SQL Server (depending on the client, but mostly MySQL.

    I have been coding in ASP (VbScript) a lot longer than PHP so it’s more fluent when I open up a blank page but I prefer PHP for reason that don’t have any bearing on this converstaion either :) I tried using the conversion software and it just really messed everything up. So when I have had to recode apps for various reason I usually just start from square one and treat it like a new project.

    I would defintly use the PHP books from Sitepoint and look through various open source projects to see exactly how to implement various functions. And the online manual at php.net is very helpful as well.

    It seems alot of these posts are picking apart the examples and not the purpose of the Blog. And that is to ease the transition between ASP and PHP.

  • John Lim

    Using ADOdb, a php db abstraction library makes porting database stuff from ASP to PHP easier, as the M’soft ADO calls can be mapped to PHP’s ADOdb.

  • Mark Ormston

    I have written a VBS script that translates from ASP to PHP with about 98% accuracy. The only changes that are needed are listed in a seperate file which easily points out where the problems exist. For example, PHP does not have a built in integer divide.

    Anyways, the way I make this work so well is by using an API that is written in both ASP and PHP, so many of the function calls are just translated over. I spend the normal amount of time writing ASP scripts, then translate to PHP and spend a marginal amount of time cleaning up the rest.

    The places that need cleaning are:
    Dealing with objects, since there’s no sure-fire way to translate them over
    Adding “global” designators to functions that refer global variables
    Adding “break” points to all switch statements. I get around this by using a
    ‘break; in the ASP everywhere I want a break in my PHP… and I don’t have the script auto-add these cause it makes it the code hard to read! I may just add it as an optional flag or something.

    It took me about an hour to fully translate a 4500 line script, an enormous savings in my opinion since the original took around 20 hours.

  • scubaguy

    Hi Mark, I would be very interested in your script. Would you be willing to share it? mkemichael AT yahoo DOT com

  • Tom Gilbert

    Mark,

    It sounds like your VB script to convert from asp.net to php is a Holy Grail for us. We have an application in asp.net and want it to run in MAC- Fortunately we are using mySQL already. So, converting it to php will allow it to run in MAC, right? I would love to see your script as well.

    Tom

  • Olle Jonsson

    An interesting field of hacking, conversions.

    Joel On Software told about converting their FogBugz software (written in ASP) to a PHP. Reqs included: automatic build process, no need for touching the PHP after it’s built.

    Key pts from Joel’s article, which you could read http://joelonsoftware.com/articles/FogBugzIII.html :

    1. A version of Hungarian notation eases the parsing work a lot.
    2. Enforced coding standards help the effort.
    3. Getting out the Big Boys’ Toolkit helps when creating a customized conversion application. All the stuff we are short on in PHP (like reflection, crazy OOP manoeuvres, and so forth), that would help to do this thing.

    (I just read the thing a few days ago, and not having been to SitePoint for ages, this blog post is what I get.)

  • Adam Morgan

    Mark, I too am interested in checking out your ASP to PHP script. I just got asked to help move my friend over and can use all of the help I can get. If you’d be cool with that, let me know: admorgan AT westmont DOT. edu ??

    Thanks!

  • farheen

    How about PHP to ASP???

  • prashant dongare

    can u plz tell me the site link which convert the php code to asp

  • prashant dongare

    hi ,farheen.
    have u know the code which convert the php code to asp

  • ajay khamamkar

    hi

  • Anonymous
  • Ted Williams

    Our developers *cough* recently did a rewrite of our entire site in asp.net, Unfortunately we choose to run all of our web services on solaris and Linux. We hired a team of contractors for the code migration. It took nearly 4000 man hours to have a running demo of the code. After that feature requests were made, unfortunate for them. They where able to implement new functionality and complete the migration in 4 months time. Since then we have hired most of the contractors and let go of over half of the previous web team. ASP.net is not a good Idea. It Forces you to use 1 platform. we have floated websites back and forth between machines since then with no problem. I highly recommend finding competent developers and not hiring an H1B with a .net cert.

  • pawan meshram

    hello how ru.

  • ajay india

    from where do i get an asp to php converter.
    is this kind of converter exists????