When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Saturday, January 06, 2001

Easy conversion to PHP for the ASP developer

By Nathan Pond


For More Information on PHP
Obviously 4Guys is not a PHP site, so for information on PHP (a Perl-based dynamic scripting language similar to ASP/VBScript) you will need to venture to sites like PHPBuilder.com and PHP.net.

- continued -

From the Webmaster, Scott Mitchell:
This article is not about PHP... it aims at helping existing ASP developers move to PHP if needed. I think ASP is super cool and am not trying to encourage anyone to switch from ASP to PHP! I understand, however, that sometimes job-related projects may require an existing ASP developer to do a bit of PHP... also it's always cool to learn about stuff. :-)

Over 4 years ago I set out to learn how I could build a web page. Within months I had immersed myself into the world of ASP and databases. It was all so cool... I wanted to use it on my personal web site.

Well, to say the least, things didn't work out like I had planned. (For a list of free ASP Web hosting sites, check out: Finding an ASP Home on the Web!) I tried a number of free ASP hosting places, very few offered databases and the ones that did were a joke. Getting a real host was just too expensive, I'm a college student, I can't afford an expensive hosting package. One day I stumbled onto a hosting site that gave me server side scripting, a database, cgi-bin, and basically everything I need for just $6 a month. The only problem was that it was UNIX, and the server side scripting could only be PHP or htmlscript, neither of which I knew.

I went ahead and got the hosting package, and set out to learn PHP. This article will entail some of the key differences between ASP and PHP, and will hopefully make the conversion process a little easier for ASP developers ready to venture into PHP.

For starters, PHP is very well documented on the php.net web site (http://www.php.net/docs.php). One key difference I noticed was the built in functionality. As I'm sure most of you know, to do just about anything in ASP you need to create an instance of an object. This isn't the case in PHP. There are built-in functions for e-mail, file manipulation, dns lookups, images, and just about everything else you can think of. Tasks such as e-mail sending can actually be done with one line of code. But before I talk PHP up too much, it should be known that I was upset with one major weakness. Version 3 of PHP had no session support. This has been added in version 4 of PHP, but not all hosts have made the upgrade yet, so be careful to look for that if sessions are important to you. I ended up just writing my own session routines.

One thing that always came back to nip me in the butt was forgetting to end each line with a semi-colon (;). PHP is much more picky about syntax than ASP. If you are a c++ programmer, this is nothing new to you. I haven't used c++ in over a year, and had grown accustumed to creating ASP with VBScript, so it was a big change for me. (Likewise, if you have been creating ASP pages with JScript (or even better still, PerlScript), then the conversion process to PHP should proceed much more smoothly...)

Commenting - There have been numerous times when I have wanted to comment out a block of code. In ASP I had to insert and apostrophe at the beginning of each line. PHP uses the same methods as c++. Here's an example:

<%
'this is a comment in ASP
%>

<?
//this is a comment in PHP

/*
This can also be used to comment out large chunks of text

this line is still commented

so is this....
*/
?>

Another huge difference is that in PHP all variables are used with a dollar sign ($). This is a little difficult to get used to at first. All variables must start with a $ as shown:

<?
  $myVar = 0;
?>

Case sensetivity - Now before you get too scared, let me explain. Variable names are case sensitive. $MYVAR and $myvar are two separate variables. However, function names and commands are not case sensitive. Meaning that

<?
   ECHO "This is a test";
?>

and
<?
   echo "This is a test";
?>

are the same thing.

The equals sign - In VBScript, and Visual Basic, the equals sign (=) does everything. It assigns values to variables and also checks conditions in if statements and loops. This isn't so in PHP. A single = will assign a value, just like in ASP.
<?
   $myVar = "This value";
?>

However, when you are using conditionals (if statements, loops, etc..) you MUST have a double equals sign == or your script will not work. This is one of the most common mistakes. A script won't work right, and you look over you code again and again and can't see anything wrong. I have wasted up to an hour trying to debug when I made this mistake.

Kind of on a related note, inequalities are tested with != instead of <>.

Connecting Strings - Strings are concatenated in PHP by the dot ., as opposed to VBScript's ampersand &.

<?
    $myVar1 = "This is a test.";
    $myVar2 = "  And I love it!!!";

    $myVar3 = $myVar1.$myVar2;

    echo $myVar3;  //  $myVar3 contains the text: 
                   //  "This is a test.  And I love it!!!"
?>

Function return values - In VBScript, to return the value of a function you simply set the function name to the value you want to return. Like this:

<%
    Function myFunction()
      myFunction = 1 'Return value
    End Function
%>

However, in PHP you use the return command.
<?
    function myFunction{
      return 1; //Return value
    }
?>

Of course, there are many more differences, but this will get you started. If I had known these points when I was learning PHP it would have saved me a lot of late nights.

Happy programming!

  • By Nathan Pond


  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article