Friday, January 13, 2006

PHP for the ASP developer

(originally by nathan pond)

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.

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 &.

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!


Post a Comment

Links to this post:

Create a Link

<< Home