Easy conversion to PHP for the ASP developerBy 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
From the Webmaster, Scott Mitchell:
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:
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:
Case sensetivity - Now before you get too scared, let me
explain. Variable names are case sensitive.
$myvar are two separate variables. However, function
names and commands are not case sensitive. Meaning that
are the same thing.
The equals sign - In VBScript, and Visual Basic, the equals
=) 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.
However, when you are using conditionals (
loops, etc..) you MUST have a double equals sign
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
Connecting Strings - Strings are concatenated in PHP by the dot
., as opposed to VBScript's ampersand
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:
However, in PHP you use the
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.