Wednesday 27 July 2011

Eclipse - How to show line numbers

To get the editor pane to show line numbers go to 'Window' -> 'Preferences' -> 'General' -> 'Text Editors' and click the checkbox labelled 'Show line numbers.

In graphical form:

Saturday 23 July 2011

PHP Development with NetBeans 7.0 - Debugging

In my previous post I talked about getting a PHP development environment set up using NetBeans and a locally installed Apache server on Ubuntu. In this post, we're going to be creating a slightly more complicated project to showcase the debugging abilities of NetBeans, which integrates very nicely with Xdebug.

Xdebug is a PHP extension which enables you to debug your code, same as if you were using something like VisualStudio with C#. In order to install and enable Xdebug on Ubuntu, you just need to run the command:

sudo apt-get install php5-xdebug

After this package has been installed, you have to enable remote debugging on Xdebug (it is disabled by default). Add the line:

xdebug.remote_enable=on

to the /etc/php5/conf.d/xdebug.ini file. In order for the changes to take effect, you will have to restart Apache:

/etc/init.d/apache2 restart

If you ever want to verify what the Xdebug settings are set to, you can see them on the default phpinfo() page.

Now that we've got Xdebug running, go ahead and create a new PHP project in NetBeans and name it 'PHPIncrement_Add':


You can leave the 'Run Configuration' screen settings set to the defaults:

In the template file, remove the php section and edit the file to include a simple form as below:

Then create a new PHP web page by right clicking on 'Source Files' -> 'New' -> 'PHP Web Page' and name the page results.php.

When results.php opens in the editor window, modify it to look like the following:

Ok, so now we have a simple web application, which takes two integers, increments them and then displays the result. We can go ahead and test this out by hitting the 'Run Project' button (or pressing F6) and we should see the following in our browser:

And after hitting the 'Submit' button, we can see the result:

So now we have a very simple, working PHP application. Now lets see how we can debug our program. We've already installed and configured Xdebug, we just need to modify a few options in NetBeans. In NetBeans, go to 'Tools' -> 'Options' -> 'PHP' -> 'Debugging' and check the 'Watches and Balloon Evaluation' checkbox:

After you check the box, you might get a warning saying "Please note that Xdebug is known to be unstable if this option is enabled". I've never had Xdebug crash on me with this option enabled so far. Also note that I've left the 'Stop at First Line' option unchecked, this is just my personal preference.

Next we can go ahead and set some breakpoints by clicking in the left hand margin of the editor window, where the line numbers are shown:

To start our debugging session, we can click on the 'Debug Project' button, which is next to the 'Run Project' button, or alternatively press Ctrl+F5.

At the start we are presented with the index.php page, so just go ahead and enter some numbers and hit the 'Submit' button. Once this is done, you will automatically switch back to NetBeans, with the debugging session started:



If you select the 'Variables' tab on the bottom pane and press the 'Step Into' (F7) button, you can see the values of $number1 and $number2:

Press the 'Continue' (F5) button and the code should keep executing until it gets to the breakpoint in the incrementAdd function. Now the 'Variables' pane doesn't show us the value of $result, however we can fix that simply by right clicking on $result in the editor pane, selecting 'New Watch...' and entering $result as the 'Watch Expression':

Note that the value of $result will be null on our breakpoint, but after pressing the 'Step Into' button, it's value is set.

Hit the 'Continue' button and you will see the results.php page displaying the results of the calculation.

So, in this post we've successfully installed and configured Xdebug and got it working with our NetBeans IDE, as well creating a simple project and going through a basic debugging session. Please keep in mind that this is a very simple example and that debugging for such a basic project would probably not be necessary. The debugger is especially useful when we have a obscure bug which we need to track down, we want to see the flow of control in a complex piece of code or maybe verify what values are being passed to the database.

PHP Development with NetBeans 7.0

In this post, I'm going to be going through creating a simple PHP project using the NetBeans 7.0 IDE and deploying it to a development Apache HTTP server, running on the same machine. All of the instructions below were tested on Ubuntu 10.04 and assume that you have a LAMP stack (although we don't need the MySQL component for now) and NetBeans installed (I've used the All bundle, which comes with PHP) and have the userdir module for Apache turned on (see my previous post).

In terms of usability, I find NetBeans to be one of the best open source PHP IDE's available, with excellent support for things like autocomplete and debugging, which make development a whole lot easier.

To start off with, open up NetBeans and create a new PHP Project:

I've set the project name to "PHPHelloWorld" and I found that NetBeans detected my ~/public_html folder automatically:

NetBeans also detected my local web server and URL settings:

For our simple HelloWorld application, we don't require any PHP frameworks:

After clicking 'Finish', we end up with the default PHP template:

Now I added some code, including a function to the index.php file. I've included a function in the code so that when you type it out you can see some of the NetBeans autocomplete features:

Finally, click on the 'Run Project' button (or hit F6) and watch as your program deploys:

Apache HTTP Server userdir module

As a part of setting up a development environment for creating web applications/sites to be deployed to Apache HTTP Server, one of the things I would highly recommend is making use of the userdir module. This module allows a user to create their own directory (under /home/[user]/public_html) and have that directory automatically be made accessible by Apache at http://localhost/~[username]/ and thus skips a lot of the headaches that are caused by permission problems.

To set up this module, first you need to create this directory:

mkdir ~/public_html

The next step is to enable the module:

sudo a2enmod userdir

Note that if you want to change the name of the directory or any other settings for this folder, you can do so by editing the /etc/apache2/mods-available/userdir.conf file.

Then, finally we just need to restart apache for the module to be loaded:

sudo /etc/init.d/apache2 restart

If it all went well, you should now be able to open your browser and browse to http://localhost/~[username]/ and see the contents of your public_html directory.

Another very important thing to mention is that by default, PHP processing is disabled on this directory. If you need to turn on PHP processing, you need to modify the /etc/apache2/mods-available/php5.conf file:


<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_value engine Off
</Directory>
</IfModule>
</IfModule>


Like it says in the commented out sections of this file, you just need to comment out the mod_userdir.c section to enable PHP on the ~/public_html directory.

Apache HTTP Server VirtualHost directive

Once of the things which always catches me out is the use of the VirtualHost directive in the configuration files for the Apache HTTP server. When you need to set up virtual hosting (i.e. more than one host off of the same IP, differentiated by the hostname) you need to use this directive. However, don't make the assumption that you can do something like:

<VirtualHost www.example.com>...</VirtualHost>

and that this will result in having a server defined for the 'www.example.com' hostname. The VirtualHost directive is used only to define the IP that this "virtual server" should listen on. It does not define which hostname it should reply to. While the above configuration is legal, the actual behaviour of Apache's HTTP Server is to lookup the hostname, convert it to an IP and use that in the directive. Functionally, it is no different than doing:

<VirtualHost [IP Address of www.example.com host]>...</VirtualHost>

Using the hostname in this part of the configuration might lead to some unexpected behaviour. For me, I added this directive with a hostname, expecting that the configuration section would only apply to a particular hostname, when in fact it matched all hostnames using that IP.

The correct way to define a hostname based virtual host is to make use of the ServerName and additionally ServerAlias directives inside the VirtualHost stanza.