How to set up Xdebug on Windows and IIS 7

Xdebug is a cool extension for PHP which allows developers to debug and profile their PHP code. It really comes in handy when you want to debug your code to fix an issue or want to find bottlenecks in your code to make it perform better. I’m making this tutorial so that everyone with any background knowledge could get started with Xdebug.
1. Get latest stable release of Xdebug from this page according to your PHP installation.
Note: If you don’t know the details of your PHP installation, use phpinfo to find out. Architecture and PHP Extension Build are the information that will help us decide. If the architecture is x86 then pick a 32-bit version, otherwise if it’s x64 pick a 64-bit edition. Compiler indicates what kind of compiler has been used to compile this PHP build. It should be either VC6 for older versions of PHP or VC9 for the newer ones. Another thing that you should pay attention to is the Extension Build being NTS or TS which stand for Non Thread Safe and Thread Safe respectively.

phpinfo without xdebug installed
2. You can skip this step but preferably, put the downloaded DLL inside your PHP extensions directory.
Note: To find out what the address of this directory is, find extension_dir in phpinfo.
3. Open php.ini in a plain text editor (Notepad++ is great for this task) and append these lines at the end of it. Make sure you use absolute addressing and your own downloaded file’s name.
Note: Again, you can find the location of php.ini in phpinfo. It’s attribute name is Loaded Configuration File.
zend_extension = "C:\Program Files\PHP\ext\php_xdebug-2.1.0-5.3-vc9-nts.dll"
4. Restart IIS by opening command prompt and typing in the following command or use Internet Information Services (IIS) Manager.
> iisreset /noforce

Restarting IIS
5. Check phpinfo one more time to make sure that Xdebug is installed.

phpinfo with xdebug installed
Xdebug is now installed and ready to use. Execute this code to see it for yourself how it overwrites var_dump and generated errors by PHP.
Note: html_errors in your php.ini should be On.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
<?php $primes = array(2, 3, 5, 7, 11, 13, 17); var_dump($primes); foo(); function foo() { bar(5, 7); } function bar($param1, $param2) { $local1 = 1; $local2 = 2; wazzup; } ?>

Xdebug overwrites var_dump and PHP errors
And the following is what you would get normally.
Normal var_dump and PHP errors
Move along to my next tutorials on debugging and profiling using xdebug too see it more in action. Also check out Xdebug’s documentation to find many many useful stuff in this regard.
Sonic & Knuckles, 2.5D Platformer Version
Have you ever wondered how Sonic & Knuckles, Mushroom Hill Zone would look like in a 2.5D platformer? I hadn’t, but if you have, here’s a good answer to end your wonder.
Sonic & Knuckles 3D - Mushroom Hill Zone
Incredible! Isn’t it?
Happy Birthday Windows 95

Microsoft Windows 95 Splash Screen
Today is the 15th birthday of Microsoft Windows 95. It was released 15 years ago, on August 24th 1995. I believe that day was a turning point in the history of operating systems. Windows 95 was really cool in those days (I guess it still is in kind of a geeky way). I was 10 years old and was really happy that I didn’t have to deal with DOS and its internal and external commands anymore. Until that day, Windows wasn’t like an operating system to me. I was really young and had no idea what an operating system actually was! So, to me Windows 3.1 was like a cool modern Norton Commander in which you could play games, draw paintings, look at a nice analog clock and make documents. Yeah! That was pretty much it. Or at least I just used it for these and had no idea what it was capable of, for all I cared was to play Commander Keen, Tom and Jerry: Cat-astrophe, North & South and Prince of Persia on my 80386 computer.
But the day finally arrived, and the world heard the “If you start it up, it will never stop” slogan. Well, that wasn’t always true and it stopped way too much. A really consice GUI operating system in which Internet Explorer 1.0 and Microsoft Home with new features were introduced shortly after. It let anyone with any degree of knowledge use a personal computer and do their own things. Whether it was playing games for kids, printing route maps for travelers, making spreadsheets for home and small businesses or doing research on the internet or Microsoft educational programs. It introduced so many cool features and UI concepts that its latest succeeder Windows 7 still has. So, keep on the good job Microsoft and thanks for making our lives easier.
Implementing The Singleton Pattern in C#
If you’re wanting to have just one instance of a class in your project, the singleton pattern is your friend. This goal is achieved by encapsulating constructor of the class we wish to have one instance of, and providing a getter for an instance of it. There are various ways to do this. I’ll provide a thread-safe version here. ApplicationSettings is just a hypothetical class which I’ll use for demonstration purposes.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// suppose this class holds settings of the application public class ApplicationSettings { // the singleton instance variable private static ApplicationSettings _instance; // the property that lazy initializes the singleton instance and also provides it public static ApplicationSettings Instance { get { // obtaining the mutual-exclusion lock to make the instance thread-safe lock (typeof(ApplicationSettings)) { if (_instance == null) _instance = new ApplicationSettings(); } return _instance; } } // private access modifier to encapsulate the default constructor private ApplicationSettings() { } // just a sample property public bool Startup { get; set; } // just a sample method public void LoadSettings() { } // just another sample method public void SaveSettings() { } }
And then in order to access the singleton object,
ApplicationSettings.Instance.Startup = false; ApplicationSettings.Instance.SaveSettings();
The very first time Application.Instance is requested, it’s gonna get instantiated. This technique is known as lazy initialization which delays creation of an object until the exact moment that it’s needed.
Installing Allegro on Visual C++ Express 2010
Allegro is an open source game development library written in C. If you’re new to C or Allegro, you probably don’t want to start trying Allegro by compiling its source code for your operating system. Therefore, it would be much easier and quicker to install the prebuilt library. You can do so by visiting Allegro.cc’s Files page and downloading the one for Visual Studio 2010, which at the moment its version 4.2.3 is the latest. Visual C++ can be downloaded from Microsoft website.
1. Extract Allegro somewhere adequete (e.g. C:\Program Files\Allegro). There should be three folders inside named bin, include and lib.

Extracting The Prebuilt Library
2. Copy contents of bin folder into System32 folder of your windows installation (e.g. C:\Windows\System32\).
3. Open Microsoft Visual C++ 2010 Express and click on New Project, or press Ctrl+Shift+N.
4. Under Win32 templates, select Win32 Console Application.
5. Enter the project name (e.g. FirstAllegro) and press OK.

Creating a New Project
6. Welcome screen of application wizard will show up, click on Next.
7. In Application Settings page, mark Empty project and make sure Application type is on Console application.

Application Settings
8. Open up Project Properties by pressing Alt+F7, or selecting Properties under Project menu, or by right clicking on project name (e.g. FirstAllegro) in the Solution Explorer window and selecting Properties.
9. Select Configuration Properties > VC++ Directories.
10. Make sure All Configurations is selected and select Include Directories, click on the arrow at the end of the row and select Edit.

VC++ Directories
11. A dialog will show up, click on the new folder icon which titles New Line and enter address of Allegro’s include folder (e.g. C:\Program Files\Allegro\include\) and press OK.

Include Directories
12. Repeat the last two steps for Library Directories but enter Allegro’s library directory (e.g. C:\Program Files\Allegro\lib\).

Library Directories
13. Select Configuration Properties > Linker > Input.
14. Put Configuration on Debug and answer yes to “Do you want to save the changes you’ve made in the property pages?”.
15. Select Additional Dependencies, click on the arrow at the end of the row and select Edit.

Linker Input
16. Enter alld.lib and press OK.

Debug Additional Dependencies
17. Repeat last three steps for Release configuration but this time enter alleg.lib as an additional dependency.

Release Additional Dependencies
18. Start having fun coding Allegro.
To check if everything’s working properly, add a new C++ file to the project.

New C++ File
Then copy and paste the following code snippet and press F5.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
#define USE_CONSOLE #include <allegro.h> int main() { allegro_init(); install_keyboard(); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); triangle(screen, 320, 40, 147, 340, 493, 340, makecol(255, 0, 0)); circlefill(screen, 320, 240, 100, makecol(255, 255, 255)); while(!key[KEY_ESC]) ; return 0; } END_OF_MAIN();
This should be the final result.

FirstAllegro Final Result
Note: With a little modification, this process can also be applied to Visual Studio 2010 as well as previous versions of this product and also previous versions of Visual C++ franchise.
Setting up multiple websites on Apache2
How cool would it be to have multiple websites on your local developing machine? Awesome, I’d say! Instead of having ugly addresses in your explorer’s address bar for your projects such as file:///home/myproject/index.html, wouldn’t it be better to have something like http://myproject/index.html? Besides looking cool and professional, this makes life much easier by solving file addressing issues that web designers and developers have to deal with while transferring a local website to a server.
This is easily achievable by installing a web server such as Apache and having all your project files copied in the default root, /var/www/ in Apache’s case. Then it’d look like http://localhost/index.html. This seems nice, but the head ache comes when you have more than one project. Your best bet probably would be replacing the project files every time you want to switch to a certain project. Or, having subdirectories that would make it look like http://localhost/myproject1 or http://localhost/myproject2 and so forth. Both of these solutions rise their own issues. A better solution would be having a hostname for each project, like http://myproject1/ and http://myproject2/.
To do so, you first need to have Apache2 installed (obviously!). Then, you must create a new configuration file in /etc/apache2/sites-available/ and name it after your hostname preferably.
$ cd /etc/apache2/sites-available
$ sudo touch myproject1
In order to set up our own website, we need to edit the newly made configuration file.
$ sudo gedit myproject1
This is what that needs to be stored in it. It simply tells the server to look for index.html as the default index page in /home/myproject1/ directory for myproject1 hostname.
1 2 3 4 5
<VirtualHost *:80> ServerName mysite DirectoryIndex index.html DocumentRoot /home/hamid/Sites/mysite/ </VirtualHost>
In this directory, there should also be another file named default which is used for the localhost hostname that you can have a look to get some ideas. To find out more about virtual host configuration files, visit this page.
Now that we have the configuration file, we should create a symbolic link to it in /etc/apache2/sites-enabled/ folder. Apache offers a command called a2ensite which does the job neatly. There’s also another command called a2dissite which does quite the opposite. After executing each of this commands, Apache needs to be reloaded.
$ sudo a2ensite mysite
$ sudo /etc/init.d/apache2 reload
Once the server is reloaded, the website is ready to be used. There’s just one more thing that needs to be done and that’s mapping the hostname to the local IP address. In order to do this, first open the hosts file.
$ sudo gedit /etc/hosts
Then append the following line to the end of it.
127.0.0.1 myproject1
All done. To see if it’s working properly, fire up your web browser and type in http://myproject1/. Repeat this process to set up more websites.
First post!
And finally here’s the first post!
I cannot even remember when was the first time I decided to launch a professional blog. A blog to share ideas, information and new things that I learn through the problems I come across every day. But unfortunately everytime I started building it, I always finished halfway, hating all the work that I had done so far, due to the fact that I’m kind of a maladaptive perfectionist and the project I defined everytime required a huge amount of work. So, this time I decided to build it from absolute nothing and elaborate it as I go along. Well, long story short… I am Hamid and I love coding.