MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to debug a PHP code

In this article we will see the most safest way to debug the PHP code and learn that FirePHP play a crucial role for quicker debugging as well as application level logging.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

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.

How FirePHP is set up?

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.

Basic Logging With FirePHP

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.

First Screenshot

Figure 1: First Screenshot

Second  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");

Introduction to Advanced Logging

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.

Grouping

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:

First 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:

Second output

Figure 4: Second output

Tables

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.

Arrays of columns

Figure 5: Arrays of columns

For the above screenshot, we clicked the label to display the data.

Traces

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:

FirePHP trace

Figure 6: FirePHP trace

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



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login