Backtrace in PHP

I have been developing in PHP from the last 4-5 years. Since the introduction of OOP concepts in PHP5, the language is getting matured and more ready for large applications.

While working with large projects, one can not avoid logging for error / debug to later diagnose the issues one might face in production. So beside logging some general information and simple errors which PHP throws and we could get from PHP we could also capture the whole backtrace for the code we are running in such case.

It could be easily incorporated in your application. Follow are some modified examples from PHP’s documentation debug_print_backtrace:

Save the above file as backtrace_include.php and call in the following file:

This will print the following:

Usually in production websites, we don’t print such trace on screen and log this in a file. We will also log such information only in a situation when an error occurs while executing the code.

Array comparison in PHP

While working on a recent project, I had to do some array comparison. I did array comparison by doing the following:

According to my requirements the above two arrays are same but if we compare it in PHP, it would not think so due to the presence of value on different indexes. To solve that, I first sorted and then compared these arrays, which gave me the required result.

PHP’s empty and isset function

The best way to check if a variable consists any value or is empty is to use empty function from PHP itself.

Though, while coding in PHP it shouldn’t be confused with isset which checks if a variable is set or not set, whether that variable is empty.

Testing Automation Setup

On a continuation to my previous post regarding Testing Automation Tools, I will try to write the process through which I went while writing the testing scripts.

The application for which I wanted to do the automated testing is a web based application written in PHP on top of Zend Framework. As Zend Framework follows the MVC pattern, we followed the same pattern. For database, we wrote our own layer and didn’t use the layer which Zend Framework provided.

For testing automation I followed the same design rules, which I followed while designing and writing this application. I wrote testing scripts for each entity in their own files. Then I called them in one main file, which I run when I want to run the regression test. Following is an example configuration file:

I treat this main script as my main configuration file. Where I declare some global variables, call those scripts which I need to run during my test run. I can include the scripts, which I need to run and can exclude those scripts which I don’t need.

I will continue with this series with further updates on how I wrote automated tests for every individual entity.