Learning PHP vs. ASP … First impressions

Learning PHP is very *easy* especially if you already know ASP.

ASP and PHP are not that very different from each other… (unlike .NET). You just have to wrap your head around a new way of thinking, and maybe learn a few new habits along the way.

Some examples:

In ASP, we enclose commands using the tag <%  %>.
In PHP, we use <?php    ?> instead.
If you’re lazy, you can use the equivalent <?  ?>, which also works… saves you from typing php everytime.

I don’t know what’s the “best practice” for doing this… <?php  or just <?
Anyone knows?
For now, I’m using <?php  ?> for my programs.

In ASP, you can name your variables simply like this:

 Name = "John Doe"

In PHP, you must use a $ sign in front of your variables. The above statement will be written as:

 $Name = "John Doe";

Also, a semi-colon “;” is REQUIRED at the end of every statement! Otherwise, you get runtime errors. I sometimes forget about this semi-colon until the browser reminds me about it.

Parse error: syntax error, unexpected T_ECHO, expecting ‘,’ or ‘;’ in /VirtualHosts/TestSite/hello.php on line 6

Sometimes, the error message given does not make it obvious you forgot a “;”.  The best thing to do is look at the statement above the line where the error is reported.  Chances are the previous statement did not have a “;”.

It’s always good to put comments in your code. In ASP, we just precede every comment line with a quote mark, like this:

 ' this is an ASP comment

In PHP, we use double slashes…

 // this is a PHP comment

This is another ASP habit I need to break when programming in PHP.

One nice feature of PHP that ASP does not have is multi-line comments. In ASP, if I want to disable a portion of my code, I have to precede every line with the quote (‘) mark.

' if not rs.EOF then
 '    varStoryTitle    = rs("StoryTitle")
 '    varStoryTeaser    = rs("StoryTeaser")
 ' end if

In PHP, I can comment whole sections using /*  */.

/*
 while ($row = mysql_fetch_array($result)) {
 echo "User ID: " . $row["UserID"] . "<br>";
 echo "User Name: " . $row["UserName"] . "<br>";
 echo "User Password: " . $row["UserPassword"] . "<br>";
 echo "<p>";
 }; // end while $row=$result
 */

Nice… I like that feature.

In ASP, if you want to output something to the screen, say the value of a variable, we use Response.Write statement.
Response.Write (Name)

In PHP, you can use the “echo” command.
echo $Name;

What else…. okay, Include files.  In ASP, if you want to use Include files, you write:

 <!-- #include virtual="/dbconnect.asp" -->

In PHP, you write Include files like this:

 include('dbconnect.php');

And one thing I discovered that PHP can do (that ASP can’t do) is you can have the name of your include file as a variable! So it will look something like this:

 $fname = 'dbconnect.php';
 include($fname);

I’m pretty sure I can think of a use for this PHP feature someday….

Also note this: In ASP, string variables must be enclosed in double quotes (“).
In PHP, you can use either double quotes (“) or single quote (‘).  So you can do something like this:

$variable = "some content";
$variable = 'some conent';
$variable = 'The movie is called "Lord of the Rings".'

In ASP, I have to write that last line as:

 variable = "The movie is called ""Lord of the Rings""."
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.