Install Xdebug

Still now I’ve found Xdebug as the most useful tool to profile the bottleneck of an API. To understand the profiling result step by step- there is Webgrind.

To install Xdebug-
Continue reading


Multi-processing using PHP

There are situations when we need to make our one-process task into a multi-process task so that we can take advantage of the operating system’s multitasking capabilities.

The basic conceptual difference between multiprocessing and multithreading– A process, is a unique instance of a program with its own memory space, own process ID number etc. Whereas a thread can be thought of as a virtual process, it does not have its own process ID, does not have its own memory space, but is still able to take advantage of multitasking.
Continue reading

Handling multiple requests using cURL in parallel

Without thread, parallelism is incomplete. Multithreading​ in PHP with pthreads:


pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects.

The issue is with this is a PECL extension. Not bundled in PHP library by default yet.

pthreads releases are hosted by PECL and the source code by » github, the easiest route to installation is the normal PECL route.

So now the question is there any minimal alternative to achieve the goal?
Continue reading

Avoid if/ else within loop

Yes, this post is about the most basic concept that using if/ else should be avoided within a loop as much as possible. Optimizing code is always important and when it’s about performance then the basic efficiency rules should be followed at first. Practically there are many situations when a loop iterates over large data set e.g. 10m; there may be differently required computation in the same loop- but if possible the loop should maintain branch prediction; avoid unnecessary if/ else.

Following simple rules lead to performance advantage certainly. Branch prediction is a simple concept to optimize a loop-
Continue reading

What is deep copy and shallow copy? How PHP uses them?

I got this question in an interview. Was aware of this conception and explained properly. But I think this topic is worth to explain, so that’s why this post is-

Shallow copy– In the process of shallow copying A, B will copy all of A’s field values. If the field value is a memory address it copies the memory address, and if the field value is a primitive type it copies the value of the primitive type. In layman’s term, shallow copy duplicates as minimum as possible. But the disadvantage is if you modify the memory address that one of B’s fields point to, you are also modifying what A’s fields point to.

Deep copy– In this process the data is actually copied completely. The advantage is that A and B do not depend on each other but this process is relatively slower and more expensive.
Continue reading