Installing and using XAMPP with NetBeans PHP About This document explains how to configure the XAMPP package with NetBeans for PHP programming and debugging (specifically for students using a Windows PC). Pre- requirements You will need a working install of XAMPP follow the instructions in the Installing and using XAMPP for PHP programming exercise ensuring that you have created a folder for your work called PHP in the My Documents folder. You should also have created and run the sample PHP program. Enabling the debugger in XAMPP The php_xdebug.dll debug file is supplied as part of XAMPP, but is not enabled in the default distribution. Check that the file php_xdebug.dll exists by looking in C:\XAMMP\php\ext To enable php_xdebug.dll, you will need to edit the php.ini (removing some semicolons) The php.ini to be edited is in C:\XAMMP\php IMPORTANT: Make a backup copy of the php.ini file (call it phpbackup.ini) BEFORE making any changes. It is very easy to make a mistake in this file which will result in XAMPP not working. If it all goes wrong the phpbackup.ini file can be used to revert the system back to the way it was before editing. Changing the PHP.INI file Open the php.ini file in a text editor (for example the free editor Notepad++). Use the search mechanism to look for the value XDebug Make these changes Edit the file to remove the semicolon at the start of the XDebug lines Change the value for xdebug.remote.enable to 1 Add the xdebug.remote_port line with the value 9000 Save the file. 1
Restart the servers The php.ini file is only used when PHP is started so you will need to use the interface to Stop and Start the servers. If the servers fail to start Close and restart XAMPP If they still fail to start Restart the whole computer If it still refuses to start at this stage, restore your backup php.ini file. Testing that the debug module is initiated Create a new PHP file called phptest.php in your PHP folder with the following lines <?php phpinfo();?> phpinfo is a built in function which displays the current configuration details of the running PHP engine. Start a browser and navigate to localhost/phptest.php to run the file. A full page of settings should be displayed. Scroll down the page to look for the (new) entry for xdebug entry for PHP. If there is no entry it means that xdebug isn t working (or isn t installed correctly) in which case follow the steps from the beginning again. 2
Installing Java NetBeans is a Java application which may not be installed by default on Windows. If you haven t used Java before you will need to download and install the latest version (available from http://java.com ). Figure 6 Next, download and install the latest version of the NetBeans IDE for PHP (the HTML5 & PHP indicated in figure 7). Figure 7 Launch the IDE and check for / install any updates (Use the Help Check for Updates) 3
Creating Projects in NetBeans NetBeans is a production level IDE (Integrated Development Environment) and is designed to work with Projects, which are collections of PHP scripts, CSS file, library and image files together. You should have a PHP folder (inside Documents) from the setting up XAMPP exercise, with the XAMPP application using this folder for its document source. To create a NetBeans project using this folder, select File New Project from the menu bar As this is a new Project with no new files, choose PHP PHP Application If you were converting an existing set of files into a NetBeans project you would select..with existing sources Use the Browse button select the PHP folder inside the Documents folder where you will be saving your PHP scripts (this is the folder the XAMPP server is using for PHP files). Give the Project a name (you should be able to leave the other settings). 4
Change the Project URL to match the XAMPP URL http://localhost When using NetBeans you should always have a Project open either through creating a new project or by using the File Open Project menu to open some previously saved project. The final part of the project creation process is to add in any PHP frameworks these can be left unselected. Using NetBeans to create a PHP file As we now have a NetBeans Project Open, any files created in the IDE will be placed in the project (i.e. inside the PHP folder). Create a new PHP file from the menu choose File New File and select PHP PHP file to create an empty file and then give it a filename. The file should open in the IDE, ready for editing. 5
Writing and running a simple PHP program Add some code to print a simple text message note the way that lightbulb icons appear in the place of line numbers where there are code hints (which you can either follow or ignore). Click the Run icon (the green triangle) to execute the file You will have to supply details to the IDE to run the file in particular the Index File may have to be changed to the filename you have just created. In a larger project with many files you may have a main file that is the first one that visitors land on (a default one for example index.php). This may be different to the file you are currently editing. The file should run and appear in a browser window The IDE will identify many common PHP syntax errors change the program to include two lines, missing out a semicolon. The Red icon indicates an error, and hovering the pointer over the icon should suggest the cause of the problem. 6
Debugging a project in NetBeans IDEs allow a program to be run in a variety of ways for testing, you can Run a program one line at a time, looking at the output and values of the variables as the program progresses Set breakpoints, which are halt points when the program is running so that variables can be examined part way through an execution To demonstrate these techniques, change the program to include more print messages and a loop Use the Debugging icon (the smaller green triangle) to start a debugging session. Use Step Into to progress down the page, running the program one line at a time Note how a green line indicates where you are in the program, moving down the code as you press the Step Into button over and over again The values that the variables take can be seen in the panel at the bottom of the page note how the value of $i changes as you progress through the loop. You should always click the Finish Debugger Session button at the end to terminate the link between MAMP, the browser and the IDE. Breakpoints With very long programs, it would become tiresome to press Step Into many time to get to a later point in the code that you wish to test to achieve this add a breakpoint by clicking on the line number so a red line appears. Pressing the Continue button will run the program to that point, from which point the Step Into / Over buttons can be used to run the code one line at a time. 7
To remove a breakpoint click on the red square again. Other debugging techniques The Step Into icon allows the execution to progress one line at a time through every line - this allows lines inside blocks of code (such as those in loops or in functions) to be individually examined. If you want to skip over blocks (for example to avoid having to go round a loop many times, or to avoid seing the inner workings of a function) use the Step Over icon. You can also place the cursor at a point further down in the code and choose Run to cursor so that you can move the debugging forward in the code. 8