Software Development

PHP for ASP.NET Developers (part 2)

In my previous post on PHP for ASP.NET Developers, I introduced the basics of how to set up a PHP environment and started showing the differences between PHP’s way of doing things from an ASP.NET developer’s perspective. In this post, I’m continuing with those differences.

Comments

Comments in PHP are very similar to C#. You can use /* to open a comment block and */ to close it later, and you can use the // format to comment to the end of a line.

PHP also allows the use of a hash symbol, #, to indicate that the remainder of the line is a comment.

Types

Booleans

In .NET a Boolean is a specific type and you had to get the type you are using into the Boolean to use it as such. In PHP things are a little more flexible. A Boolean can be true or false, as you might expect. But there are special cases with other types. For example, a string can be treaded as a Boolean (in an if statement, for example). If the string is empty or contains just “0” (zero) then it is treated a false. Any other value is true. Numbers have a similar effect. Zeros are treated as false and others are treated as true. null is also treated as false.

 

Dates

There are two ways to get the current date. The first is with a call to time() which returns a Unix timestamp (an integer representing the number of seconds between 00:00:00 on 1/1/1970 and the date in question). The other way is to use date() with just a format string which returns the current date formatted as specified. You can also call date and pass a timestamp and have it convert the timestamp in to the specified format. For example:

$nowTimestamp = time();
echo "<p>The time now is ".$nowTimestamp."</p>";

$nowDate = date('d/M/Y H:i:s');
echo "<p>The time now is ".$nowDate."</p>";

You can find the details of the formatting specifiers at php.net.

If you want to convert a specific date into a timestamp then you can use the function strtotime() to perform the conversion. It is quite powerful in ways that I didn’t expect, you can set the string to values such as “next Tuesday” and “yesterday”. However, the parsing of specific dates can be tricky. If you are using the American format which puts the month first you separate the components with a slash, if you are going for the European sequence which puts the day firsr then the month, you must use a dash as a separator.

For example:

$weddingTimestamp = strtotime('13-4-2012');
echo "<p>My wedding is ".$weddingTimestamp."</p>";

$weddingDate = date('d/M/Y', $weddingTimestamp);
echo "<p>My wedding is ".$weddingDate."</p>";

There is a DateTime class also available which can be used.

$now = new DateTime();
echo "<p>Today is ".$now->format('d-M-Y')."</p>";

Collections (arrays, lists, dictionaries, etc.)

PHP allows you to create an array simply by referencing the first item.

$myArray[0] = "Hello";
$myArray[1] = "World!";
echo $myArray[0].' '.$myArray[1];

And if you don’t want to have to keep a count and just keep appending on the end, you can do this:

$cities[] = 'Edinburgh';
$cities[] = 'Glasgow';
$cities[] = 'Aberdeen';
$cities[] = 'Dundee';
$cities[] = 'Inverness';
$cities[] = "Stirling";

Or, in an even more compact manner, like this:

$cities = array('Edinburgh', 'Glasgow', 'Aberdeen',
    'Dundee', 'Inverness', 'Stirling');

However, there is much more flexibility here. You can define the contents of a Dictionary like collection in the same way. Like this:

$capitals['Scotland'] = 'Edinburgh';
$capitals['England'] = 'London';
$capitals['Wales'] = 'Cardiff';
echo 'Scotland's capital is '.$capitals['Scotland'];

Like regular arrays, there is a compact way of expressing this too.

$capitals = array('Scotland' => 'Edinburgh',
    'England' => 'London', 'Wales' => 'Cardiff');

You can also use the formatting options that defining a string with double quotes permits in order to put the value of an array element into a string. Like this:

$capitals = array('Scotland' => 'Edinburgh',
    'England' => 'London', 'Wales' => 'Cardiff');
echo "The capital of Wales is {$capitals['Wales']}";

Finally, to remove an item from the array you can use the unset

unset($capitals['England']);

However, be aware that the indexes don’t move up if you remove an element, so for example, the following will fail:

$stuff = array('Zero','One', 'Two', 'Three', 'Four');
unset($stuff[1]);
echo 'Element 1 = '.$stuff[1];

5 thoughts on “PHP for ASP.NET Developers (part 2)

  1. ASP.NET and think they can build a website, but in reality, a website is a completely different thing to say a banking system or email client.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s