PHP for ASP.NET Developers (part 1)

For some reason that I can’t even explain to myself, I decided it would be a good idea to learn PHP. It is apparently used on roughly 60% of web sites, so it is certainly popular.

Quick Start – Install XAMPP

To get going quickly I downloaded XAMPP, which is an installation that contains all the bits I’d likely want to use to get started with. It contains apache, php and mysql in one handy package that is easier to install than trying to get the bits going separately. It also hopefully reduces the frustration of getting going which can be terribly off putting for someone new to a technology.

If you have IIS running already on your machine, I’d turn it off if you are installing XAMPP (at least during the installation and configuration phase) as the two technologies will request use of port 80 (HTTP). If you do want to run both at the same time, you are going to have to ensure that they use different ports.

if you have IIS running when XAMPP installs, then it won’t start Apache, and the use interface control panel that it provides doesn’t seem to keep up with the running state effectively so if you turn off IIS then ask it to start Apache it appears not to do so (although it does also put little warning messages up saying that if you are running 64bit to disregard the previous warning…. and I am running 64bit) yet it will serve pages.

Anyway, I didn’t install XAMPP to prepare production ready code, I installed it to learn, so I’m going to ignore some of these little foibles. Over the course of time I expect to become familiar enough with Apache, PHP, and MySQL that I’ll understand how to install them properly so that it can be used in a production setting.

Choosing an IDE

There are many IDEs to choose from, or you can go with notepad if you like. I’d rather not be stuck with notepad so I installed NetBeans 7. I don’t think it is necessarily the best, but it is a good starter. If you want a quick comparison then you can find a PHP IDE comparison chart on

Some basic differences between PHP and ASP.NET

Code Render Blocks

In ASP.NET you can open code render blocks using <% and close them with %> In PHP you can start with <?php and end with ?>. Some implementations also allow a shorter version of the opening tag which is <?



PHP statements are case insensitive, although variable names are not. For example, the following two lines are equal:

echo "<p>Hello World!</p>";
Echo "<p>Hello World!</p>";



In PHP all variables start with a $ sign. While statements in PHP are case insensitive, variables are not. Also, you don’t have to declare a variable with a specific type, you can just assign directly to it. You can also assign a value of a different type to the variable from the one it started out with.

However, if you do attempt to use a variable that is unassigned then you may end up with a message like this on your page:

Notice:  Undefined variable: name in C:devindex.php on line 11

If you have a variable that you no longer want, you can explicitly unset it, which means the variable will cease to exist. To unset a variable use:


You can, of course, also set the variable to null. In this case the variable will still exist, it just won’t have a value.

You can check to see if a variable is set (exists and is not null) using isset($myVariable). If the variable has never been assigned, or is null, then it will return false.



Strings can span lines (in C#, you have to prefix the string with an @ to allow this). For example:

echo "<p>Hello World!</p>";
echo "<p>Hello

Will produce output that looks like this

<p>Hello World!</p><p>Hello

Strings are handled differently if you enclose them in single or double quotes. If you enclose a string in single quotes it is treated as a literal string. If you enclose a string in double quotes, then you can embed variable any they’ll be parsed into the string…. so you can treat is like a simple form of string formatting.

If you want to put something in a string that looks like a variable name then you have to escape the initial $ sign at the front. For example:

$name = "Colin";
$message = "$name is a variable";
echo "<p>The first char is not a $ but a ",$message[0],"</p>";
echo "<p>",$message,"</p>";

If you have an exceptionally long string PHP has a neat little trick to allow you to specify a string of, I guess, limitless length. It is called a “heredoc”. How you use it is that you put in three left pointing chevrons followed by a string that denotes the end of the heredoc. That terminator string is something that will never appear in the heredoc and therefore will be safe to use as a terminator. For example:

$myString = <<<ThisIsMyTerminator
I can put as much as I want in this space
using any formatting I want, so long as I never
put in the termination sequence I specified at
the start until I am ready to terminate the
string. Like this

Remember that you have include the semi-colon, just because the heredoc allows you to suspend all the other PHP parsing rules doesn’t mean the statement can live without its semi-colon at the end.


Variable variables – Or pointers to variables

PHP has a concept of Variable variables… Which is a variable that contains the name of another variable. It is a bit like a pointer to the actual variable you want to use. So, you can do this:

$myVar = "name";
$$myVar = "Colin";
echo "<p>$myVar is ",$myVar,"</p>";
echo "<p>$$myVar is ",$$myVar,"</p>";

Note that to dereference the “pointer” you use a double $ sign in front of the variable name that points to the actual value.

The result of the above is:

$myVar is name
$$myVar is Colin

If you want to get the value being pointed formatted into a string, you need to put the pointer name in braces, like this:

$myVar = "name";
$$myVar = "Colin";
echo "<p>$myVar is $myVar</p>";
echo "<p>$$myVar is ${$myVar}</p>";



To declare a constant in PHP there is a define statement. For example:

define(“copyright”, 2012);

Unlike variables you don’t need the $ sign in front when using the constant, however, you can’t put the constant in to strings like you can with variables.

define("year", 2012);
define("holder", "Colin Angus Mackay");
echo "<p>Copyright &copy; ",year, " ", holder, ". All Rights Reserved.</p>";


  1. Chris says:


    The example of Variable variables is very confusing and doesn’t demonstrate the purpose. Basically it looks like you have two different variables with the same name, but distinguished by the fact that one has a single $ and the other has two of them. May I suggest you edit the example to include something like this?

    $myVar = “name”;
    $$myVar = “Colin”;
    echo “$myVar is “,$myVar,””;
    echo “$$myVar is “,$$myVar,””;

    echo “$myVar is $name”;


Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s