The article will teach you the process to debug PHP code that is considered one of the most important reviews being the technical manager of the project.
A debugging method should be used that does not show details to non-programmers via the help of the web page. The safest and secure method of debugging makes it necessary for you to configure your IDE to use a tool that could be Xdebug or Zend Debugger. On the other hand, in the absence of making use of a debug environment that is completely configured, we can take the assistance of FirePHP.
FirePHP is a PHP library that is meant create generate console messages in the browser from your PHP code. These Console messages are usually created by JavaScript whereas on the other hand FirePHP sends same sort of messages. This includes HTTP headers with the response from the server. We have lot of browser extensions that generally act as proxies for the JavaScript console methods and it converts these headers into console log messages. The reason why this tool is considered to be safer is that when making use of these methods such JavaScript’sconsole.log(), the requirement is to make a good amount of effort so as to view the debug messages.
In case you are using a Firefox, you need to install FireBug and that too for an expanded set of tools in order to debug your web applications. Post this it is required to install the FirePHP extension to extend FireBug and then intercept the FirePHP messages. For people making use of Chrome, just install one extension FirePHP4Chrome.
People working on different error level logging must have seen the arguments on the differences between the types of errors that should be “warn” versus “error.” It is adequate to mention that there may be reasons in order to differentiate a message that is required to be send across to the console in an altogether different way as compared to a standard message.
Log, info, warn and error are the four types of messages that is supported by the FirePHP protocol and depends on you to select the proper one for your environment and the context of your debugging/logging. The code mentioned below represents the process to include the FirePHP library and run each of the logging types.
<?php
require 'FirePHPCore/fb.php';
FB::log('Log message');
FB::info('Info message');
FB::warn('Warn message');
FB::error('Error message');The purpose of the above code is to include the core FirePHP library and then call several of the logging methods. And we have multiple ways to call the library methods. Majority of the people make use of the static methods for the reason that they generally want to use FirePHP in only one line to send debug information.
Below, you’ll see screenshots with the output on both Firefox and Chrome.

Figure 1: First Screenshot

Figure 2: Second Screenshot
It can easily be notices that Firefox’s display is a much better than the Chrome one for the reason that we don’t have an icon for “info” in Chrome version. This is for the reason that console.log() in Chrome does not support the icon yet.
Now let’s incorporate a label to the log messages in order to make the logging information more useful. This is being specified as the second parameter of the logging methods.
<?php require 'FirePHPCore/fb.php'; $turtles = $zooService->fetchAllTurtles(); FB::info($turtles, "All Turtles");
In order to generate one-time log messages, FirePHP is a great tool but we need to look at some more advanced features that will make FirePHP really shine. Let us take a look at message grouping, tables, and traces.
Assume we have a bit of logging that is required to be done in a loop. It could potentially get out of hand and make the console scroll that would leave us to put that it a group. This will make the job easily collapsed and expanded. The below sample code comprise of four entries of information that needs to be grouped together.
<?php
equire 'FirePHPCore/fb.php';
$specialHashes = array();
for ($x = 0; $x < 5; $x++) {
$specialHashes[$x] = sha1($x . 'somesalt');
FB::info($specialHashes[$x], "Hash #" . $x);
}Here is the output:

Figure 3: First output
We need to group the log messages together and allow for it to be collapsed. The below code includes some modification done to the code so as to include grouping methods built into FirePHP.
<?php
require 'FirePHPCore/fb.php';
$specialHashes = array();
FB::group('Special Hashes');
for ($x = 0; $x < 5; $x++) {
$specialHashes[$x] = sha1($x . 'somesalt');
FB::info($specialHashes[$x], "Hash #" . $x);
}
FB::groupEnd();And now, the new output:

Figure 4: Second output
FirePHP can send tabular data very easily and for the reason that we have a Firefox’s console.table() method , the data is being displayed beautifully and is easy to understand. In order to send table data in FirePHP, we use an array of columns. However, the first element of the array must be the column names. Making use of the special hashes example, let’s log them as a table:
<?php
require 'FirePHPCore/fb.php';
$specialHashes = array();
for ($x = 0; $x < 5; $x++) {
$specialHashes[] = array($x, sha1($x . 'somesalt'));
}
$headers = array('Hash #', 'Hash Value');
$logTable = array($headers) + $specialHashes;
FB::table("Special Hashes", $logTable);
For the reason, the first element of the array sent to the table() method needs to be column labels, a temporary variable is created to do this task. The table() method takes a Label parameter which is followed by the array of columns.

Figure 5: Arrays of columns
For the above screenshot, we clicked the label to display the data.
At the time of debugging PHP code, traces can really play a vital role. Irrespective of if you want to couple them with a log message showing a variable’s value at specific point in the code or you need more information from an exception, trace is where it’s at. One can call a trace anywhere in the PHP code with FirePHP as shown below:
<?php
require 'FirePHPCore/fb.php';
FB::trace('Simple Trace');Here is the output:

Figure 6: FirePHP trace
When the output was displayed, we just accessed label of “Simple Trace” in order to display the entire trace.







See the prices for this post in Mr.Bool Credits System below: