Category: General

PHPNW09 A Review

18 October, 2009 | General | No comments

Last weekend saw the second annual PHPNW conference, and it was an excelent conference. There were some 200 people attending the event and we got to see some interesting and informative talks. When I arrived at the talk I received a bag with some brochures in it as well as a KitKat (which I ate for breakfast) and a years subscription to PHP|Architect. Everyone at the conference was also fed very well for lunch and dinner and Sun sponsored a free bar at the end of the first day, which was nice.

What I thought I'd do is go through each of the talks that I attended and copy in my responses from the joind.in reviews that I have been posting during the week, but also embellish them with further thoughts and comments. Also, joind.in seem to have deleted one or two of my reviews so I will have to write them from scratch anyway.

(more...)

Written by Philip Norton.

Using Netbeans PHP Code Templates

11 September, 2009 | General | No comments

Netbeans is a great IDE and with every version lots more features are introduced that make it even better. One thing that I like to use is the code templates, which have been available from version 6.5. Code templates allows you to type a simple command and get a section of code. What commands you can use depend on what version of Netbeans you are using and which programming language you focused on. As a PHP developer I usually download the PHP version, which comes with a set of PHP code templates. To try one out go into a PHP file in Netbeans and enter if followed by a tab. Netbeans will automatically change this into the following:

if (condition) {   ; }

You might notice that Netbeans puts your cursor over the condition part of this statement, you can now type in the condition part of the statement without having to move the cursor into the right place. Hitting enter after you have typed something here will take you to the next line, just before the semi colon.

Note that you need to type if followed directly by the tab key, you can't enter any other key presses between these or it will not work. To look at the different code templates available go to Tools > Options, click on the Editor icon and then click on the Code Templates tab. You should see a dialog that looks like this:

Netbeans Code Templates

Netbeans Code Templates

The code templates are split into different languages, the diagram above shows the PHP language selected and start of the PHP list of code templates. To see what a template creates just click on the line, it will show you in the white box below.

It is also possible to create your own templates that can be used just like any other template. I use the following block of code quite a bit:

echo '<pre>' . print_r($someArray, true) . '</pre>'

Click on the New button to the right hand side of the table and you will see a dialog box, this box is for you to type in your abbreviation. In this box I typed in the following:

print_r

This dialog then takes you back to the code templates screen and inserts a line with your abbreviation into the table and highlights it. In the text area in the lower half of this screen enter the following:

echo '<pre>' . print_r(${cursor}, true) . '</pre>'

Finally on this screen, if you want to change the keypress that completes your code template then you can do it at the bottom of this dialog. You can only change it to space, shift+space or enter. Once you are happy click ok to save the templates you entered and get back into the editor.

Now when you type print_r and hit the tab key (if you didn't change it to something else) Netbeans will print out the following:

echo '<pre>' . print_r(, true) . '</pre>'

And put the cursor in place of the ${cursor} string we defined earlier. The ${cursor} string is a special parameter used by Netbeans to place the cursor at that point. However, anything you enclose in ${} will be seen as a place marker and will allow you to enter a bit of code and hit enter to continue to the next place marker.

You can create code templates for single lines of code like the above, or for much more complicated sets of code like class testing templates.

Written by Philip Norton.

Virtualization With VirtualBox

25 March, 2009 | General | 2 comments

Virtualization is basically a term used to describe the creation of a computer in software. The main benefits of which are that if you want to try out an operating system or test client server communications you don't have to get multiple computers. You can simply create a few computers virtually, which will act just like the real thing.

There are quite a few virtualization products available, some are free and some cost quite a bit of money. After messing about with quite a few different virtualisation products other the past few weeks I have uncovered a great bit of software called VirtualBox from Sun Microsystems.

VirtualBox

After installing VirtualBox it took me only a few minutes to set up a new virtual machine. I decided that I wanted to create a virtual machine with Ubuntu installed on it, so I selected the options that optimise VirtualBox for this system. Note that this doesn't set up a virtual machine with Ubuntu already installed, you have to go and download it yourself! Here is a screenshot of the main interface, which is very easy to understand.

VirtualBox interface

VirtualBox interface

Using virtual machines in the past I have found that to install anything can take a very long time. Installing Ubuntu took not more than 30 minutes, at which point I was presented with the bootup screen.

VirtualBox Ubuntu Boot

VirtualBox Ubuntu Boot

Here is another shot of the operating system fully loaded.

VirtualBox Ubuntu Loaded

VirtualBox Ubuntu Loaded

VirtualBox also supports the creation of snapshots, so if you have a fresh install of an operating system that you want to mess about with (or break) then you can create a snapshot of the virtual machine before you begin. This way, after you trash the system, you can revert back to a snapshot of the system before you started messing about.

The only trouble I had was figuring out how to interface the virtual machine properly with my network, but after reading the documentation this all became very clear.

Overall VirtualBox is a great bit of software, with a detailed and comprehensive manual. I just can't believe that this software is free!

Written by Philip Norton.

Writing Function Code To Be More Readable

1 October, 2008 | General | No comments

Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion. No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in. The reason is simple. Lets say you had a function that took in a couple of parameters.

function myFunction($intNum1,$intNum2){  // function does something }

Normal practice is to check the parameters to make sure that they are what you expected them to be before continuing on with the rest of the function. Lets say that we only want the numbers to be in a range. If they are not in the range the function should return false. Many programmers might start of writing something like this.

function myFunction($intNum1,$intNum2){  if($intNum1 > 0 && $intNum1 < 1000){   if($intNum2 > 0 && $intNum2 < 1000){    // do something with the numbers.    return $value;   }else{    return false;   }  }else{   return false;  } }

This code will still produce the desired effect of checking to see that the numbers are within a range, but there is a simpler and more elegant way of doing this. For example, what if you wanted to add a third parameter, or a fourth? Having nested if statements is hard enough, but when you get to four levels things get a bit silly.

Here is the same code, but refactored in a way that allows for better readability and maintenance. Notice that the if statement logic has been reversed.

function myFunction($intNum1,$intNum2){  // check parameter 1  if($intNum1 < 0 && $intNum1 > 1000){   return false;  }  // check parameter 2  if($intNum2 < 0 && $intNum2 > 1000){   return false;  }  // do something with the numbers.  return $value; }

This way, we get past all of the logic checking before getting down to what we want the function to do, and it is readable. Also, if you wanted to add another parameter you would just add another if statement to check that parameter. There if no juggling of nested statements or complicated syntax.

The above function also doesn't take into account the fact that the parameters might not be numbers, additional checking can be added at the top of the function to account for this.

I have posted this in the general category because I have used this technique in at least 3 different languages since I started doing it in PHP. All of the code written here is in generic PHP like syntax, but the idea stands. Hopefully, you should get something out of this as well.

Written by Philip Norton.

The Google Chrome User Agent

3 September, 2008 | General | No comments

As the new Google web browser was released last night (I'm writing this post using the new browser) I thought it would be good to update our readers on the user agent string that this web browser has.

The user agent of any browser can be found out by using the userAgent property of the navigator object. This is available in most modern browsers and is thankfully also present in Google Chrome.

navigator.userAgent

As an example the user agent for FireFox 3 on a Windows XP machine looks like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1

Using the same code, and the same machine, the user agent produced by Google Chrome is as follows.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

So detecting it should only be a case of looking for the word "chrome". Just like this:

var ischrome = navigator.userAgent.indexOf("Chrome")? true : false;

If you want to see what the user agent is on your machine then past this code into a web page and hit refresh. It is quite a basic bit of code and should work in most browsers.

<span id="useragent"></span> <script type="text/javascript"> document.getElementById('useragent').innerHTML = navigator.userAgent; </script>

Written by Philip Norton.