Convert Asp to Php

Converting ASP code to Php is a tedious process. Here is a tool and some conversion steps to help ease the painful process.

Editing Tool to help conversion

HTML-Kit is a free editor that has some special functionality to make the conversion easier. It has plugins for Php (and ASP) and text conversion tools (abc on the Tools menu). Download plugins. To install a plugin, click Tools, Install, Install PLugin in the Html-Kit menu. Add the functionality you use most to the Favorites tab by right clicking it in the menu and choosing Add to Favorites. Use the following shortcuts in Html-Kit to make the job easier:

  • Ctrl-c to copy
  • Ctrl-v to past
  • Ctrl-x to cut
  • Ctrl-s to save
  • Ctrl-c to copy
  • F6 to repeat the last action (last plugin, not these control keys or their functionality)
  • Ctrl-z to undo
  • Ctrl-y to re-do
  • Use the drop down arrow beside the folder and internet folder icons to open recent documents
  • use alt-end to create an ending tag for the current tag. Only works for the last tag, not for nested tags.
  • Use Ctrl-k to create address/anchor tags

Steps for Converting ASP to Php

ASP code is indicated in red and PHP code in green in this tutorial.

  1. Refer to ASP tutorial and PHP manual for well organized documentation of the two languages and ASP/PHP Crossreference or PHP, Javascript and VBScript Language Summary for similiar code in ASP (vbscript) and Php.
  2. Convert <%=variablename%> to <?php echo $variablename; ?>
  3. Convert <% to <?php and %> to ?>
  4. Convert structure

    • If then else structure

      • replace if condition with if (condition)
      • Convert = before the then to ==
      • replace then with {
      • replace else if with } elseif {
      • replace else with } else {
      • replace end with }
    • Case structure

      • replace select case variable with switch ($variable) {
      • replace case value with case value:
      • replace case else with default:
      • replace end select with }
    • For next structure

      • replace:
        For i=1 to 10
        some code
        for ($i = 1; $i <= 10; $i++) {
        some code;
    • For each structure

      • Replace
        For Each variable in array
        some code;
        foreach ($array as $variable) {
        some code;
    • Do while structure

      • Convert Do While i>10
        some code
        to while ($i > 10):
        some code;
      • Convert Do
        some code
        Loop While i>10
        to do {
        some code;
        } while ($i > 10);
      • Replace exit do with break;
  5. Convert variable names: name to $name
  6. Add ; to the end of every statement
  7. Screen output: convert response.write "Your message" to echo "Your message";
  8. Form processing

    • If used POST, add import_request_variables("P", ""); to the first Php code block
    • If used Get, add import_request_variables("G", ""); to the first Php code block
    • Convert input of POST variables by changing
      to $formvariablename (for POST variables)
    • Convert input of GET variable by changing
      to $formvariablename (for GET variables)
  9. Server variables: convert Request.ServerVariables("HTTP_HOST")
    to $_SERVER["HTTP_HOST"]
  10. Convert structure of functions defined within the program
  11. Convert structure of subprocedures (subroutines)
  12. Convert system functions. Here are a few ASP and corresponding Php function names with links to the php manual description of that function (underline indicates a link, while green still indicates Php). See Php Function Reference and ASP functions for lists of functions organized by type.

    • chr - no conversion needed
    • instr to strpos (case sensitive) or stripos (case insensitive)
    • mid to substr
    • replace to str_replace
    • trim - no conversion needed
  13. Convert
    <!-- #Include File="url" -->
    include 'url'; and place it within a Php script (<?php ?>)
  14. Convert database acess
  15. Convert comments - replace ' with // - Note do not replace all ', only those indicating comments (Php also supports multi line comments surrounded by /* and */)
  16. The above will probably catch most of the changes needed to your code. Now it's time to try to run it. Debug the code and remember to eliminate the initial // in the following when debugging and replace it in your public version of your code.

    • Add
      // print_r($_POST); // Uncomment to print contents of POST variables for debug
      and delete the initial // when you want to print all form variables
    • Add
      //error_reporting(E_ALL); // Uncomment to report all PHP errors, keep commented out in public version for security reasons
      and delete the initial // when you want to display errors

Common Php Errors and their Solutions

  1. Parse error: syntax error, unexpected '='
    The variable name before the '=' probably does not begin with '$'
  2. syntax error, unexpected '{', expecting '(' on an if statement
    The condition must be surrounded with parentheses, not brackets - use '(' and ')'
  3. syntax error, unexpected '}'
    There is no ';' at the end of the previous statement or an extra '}' appears previously or this is an unneeded '}'
© Copyright 2023 Dotty Storer