Archive for the ‘PHP Notes’ Category

2 things you should never do as a programmer

You will find lots of guides and articles on the internet about what you should do to be a good programmer. A lot has been written on what we should do to excel in any field.

In my view, its not only what we do which make us good in our profession. It mostly depends on what we should not do which make us excel at our work.

In this article I am making short list of two things which any programmer should not do.

1. Stop Reading about new things

If you think you know a lot about programming because you are an expert in one of the so called mother language, then you are making a big mistake. Even languages which were marked as stable and labelled as mother languages like c and c++ are changing a lot. At least new libraries and being added to them. So you should never stop reading about new things. Subscribe to few good magazines or journals, even if you do not have time to read all articles and magazines. You will be at least aware of what is going on around.

2. Coding before architecture

Never start coding before deciding on the architecture of your application. Its a huge mistake many new comers as well as so called experience people do. I can tell you from my experience in projects, its architecture which makes a project success or failure. Starting code before taking decisions and making plan for project execution is like going for ride when you really dont know where to go. Read about design patterns. They are documented solutions for common problems. Try to use them.

Categories: PHP Notes, Resources List Tags:

What are some limitations of PHP?

By   | Comments off

Recently I answered a question on quora. I am reproducing it here again.

In my view question is not complete and does not provide context which has to be evaluated to answer.

So I am writing this answer based on two assumptions.

First of all I am assuming that questioner is evaluating few programming languages and wants to make a decision which language to use or learn?

One of the major feature of PHP is also its biggest limitation. It is loose type and does automatic data type conversion in most of the situations. It does not force you to be good. You can easily write a bad code and it will not give you any warning or notice, but this does not mean that you can not write good code in PHP. So if you ever found a good PHP programmer, praise him/her because he/she is doing good programming without being under a hunter of strict compiler or interpreter.

PHP is more suitable for web development and shell scripts (yes it is quite good at command prompt also). You should not consider this for desktop applications. PHP-GTK is available but still far away from being productive. So one limitation is lack of desktop application development support. You can also not do mobile programming although you can run PHP on an Android device start [PHP for Android]

PHP supports almost all good features and data types but it does not force you to use them (.. and falls back to default behavior) which sometimes can be source of bug introduction in your applications.

As far as leaning is concerned, PHP is easy but I will suggest learning another strong typed language like C or Java along with PHP (at least basic knowledge must be there) if you really wants to go to a good level in programming.

Second assumptions, I am making that questioner wants to gather points to do fight with the PHP guy on the block, in that case I would suggest, Please do not waste your time, No language is without some shortcomings in some specific context.

So choose your language based on your need. PHP is best in some cases and worst in some cases

PHP is growing and is on a fast lane in new features introduction, so we can hope that it will extends its reach to more different verticals in near future.

Drupal vs Zend : which one should be used for application development?

“You have done that specific site in Zend, don’t you think our application is also suitable for Zend instead of Drupal”, I just got this email from a prospective client.

This question has been asked to me many times. I am a big fan of Drupal as well Zend, so this question always put me in my difficult zone. As I know both, I really can not take a stand against any one of them. Technically speaking, both are capable of handing any kind of project, although both have their pros and cons.


There is proverb, “All roads leads to Rome”. This same anatomy can be used in the tech decisions, if you are adamant about a technology you can do anything in that. Its just a matter of time and money, in short we can say its matter of resources. So I really can not state that a ‘particular’ task in not possible in Drupal or Zend. I can only state how much time a task will take in Drupal vs Zend or vice versa.

Lets try to analyze some of the common benefits of using Drupal and Zend. We will try to see which kind of application is suitable for Drupal or Zend.

Zend is a framework which provides MVC structure and many libraries to work with different components and services. These components and services allows you to build an application with ease. You can keep your concentration on the business logic and requirements of application instead of worrying about underline technical details of functional implementations.

Zend gives you a structure where you can implements many design patterns which you may have read in design pattern books. Using Zend you can directly use those classy patterns (thus takes benefits of them) in your applications without writing low level implementation code.

I am trying to enlist few requirements of  almost all applications and my views on them in regards to Drupal and Zend

Time frame

Turn around time for Zend application is usually higher than Drupal applications. In Drupal you have a running CMS with lots of available modules which  gives you functionality out of the box. So on time frame front Drupal scores really high.

Zend applications has greater control over design aspects as it is programmed from scratch. Drupal on the other hand has a defined structure of elements which needs to used in certain ways. If you want to get rid of those structures, you may loose some benefits of drupal framework. Here Zend scores higher points as compared to Drupal, although a good designer with programmer support or programming knowledge can do wonders with Drupal. CSS manipulation is quite easy with Drupal.

Features Development

On this point, Drupal scores really higher as compared to Zend. Drupal has more than few thousands modules with different kind of functions. It also has concept of Site or Installation profiles where you can get a set bunch of modules for a particular use.

Zend on other hand is only a framework with libraries, it can help you to build applications but it can not gives you out of the box solutions. Zend version 2 has taken a big leap in this direction as module framework has evolved a lot with this release. So we can hope for some great modules in near future (We are also working on some such modules at our company which we will release in near future). There is another aspect of this feature, Drupal while having lots of modules also confuse potential customers or developers. They normally needs to do some hit and trial before they can get a good usable module.


When it comes to branding, Drupal scores less than Zend. Drupal being a CMS does have less respect in eyes of policy makers as compared to one which Zend enjoys. Zend seems to be perfect tailored solution while Drupal comes as something modified/managed solution. This is something which is beyond any technical explanation. So I will keep my views on this point with myself only. Just to state, I did not like such kind of perception building, but you can never argue with policy makers or people who has powers to take decisions.


As time frame is better with Drupal, it is obviously cheaper to build than Zend applications.

Most important factor in Drupal vs Zend decision is the requirement document of the application in hand. For example, application may be quite unique in its requirements, and you may not have any available module for it in Drupal. In this case, you loose time frame benefits.

So final decision should be taken with care with consultation with client.


I have given following reply to my this client

We have worked with both Zend and Drupal for different projects. In some projects we have even used both Zend and Drupal together.

Actually tech decision depends on many factors like budget, cost, clients internal policies and application requirements. Zend is a framework, while Drupal is also a CMS with good framework for a foundation of good application. Different modules also supports us. As I have mentioned in my estimation email, we are also going to get benefits of our earlier work on similar things.

In your application structure, Drupal is perfect choice. Having said that I would like state that your project is also doable in Zend, but in that case we will loose the benefits of ready made modules and our earlier work.

Again, I will repeat ,we can handle both, but economically and time wise Drupal is best choice for you if you do not have issues with its GNU  GPL license.


I have done extensive work in both Zend framework development as well as Drupal development. Drupal and Zend both are feature rich and secures platforms if you can develop and deploy your apps with a proper planning. If you feel that an expert with good experience in both Zend and Drupal development may help you in your decision between Zend and Drupal. Please feel free to contact me , you can also chat with me if I am online.

Why randomization is a difficult algorithm to implement?

If you are doing programming, You must have used random numbers. They are useful in many situations when you want to give some randomization to your program output like lottery draw, random banners generation, random user display etc.

In PHP rand function is being used to generate random numbers, but is it really random number generator? Take a look at the following code.

< ?php for($i=0;$1i<50;$i++) { print rand(); } ?>

What this function will do? If you are thinking that it will produce 50 random numbers, you just need to run this code. It is very likely that you will get the same number fifty times. Why rand function is not generating random numbers? Lets me introduce another function srand which is short for seed rand or seeding rand.

What is seeding? seeding in random number generation is they key to uniqueness. If you are not feeding a good seed to your random number generator, you are out of luck.

Why it is so difficult to implement random number generation algorithm?As per definition of algorithms, they are set procedures to solve a given problem. they can be repetitive and usually they are consistent in number of steps, in short they have a pattern. Random number on the other hand are supposed to break pattern. Random number generation is actually finding a pattern which should output a result which should break pattern. Thats why it is difficult.

Seeding is important to random number generation. Many algorithms even require more than one seed to do generate better random number.

In computer science, we are normally dealing with pseudorandom random numbers. Liner congruential generator also known as LCG is one of the oldest and best-known pseudorandom number generator algorithms

Categories: PHP Notes Tags: ,

7 things every web developer should know about Linux administration

Web programming is not only about how you develop your applications, its also about how you deploy your application or website.Although control panels are being provided by almost every shared hosting company using which we can easily deploy our applications but as soon as application grows in terms of size or scalability we need to put that on virtual dedicated or dedicated or in a cloud server like amazon EC2 .

Linux system administration is specialized field in itself but if as a programmer you also know how to handle some common tasks you can get good turn around as well as can optimize your application for performance. Easy and efficient deployment process can also save your debugging and testing time. In this post I am going to list 7 Linux administration tasks which a programmer should know to be with ease while deploying or planning deployment of applications.

1. SSH connections
This is first and foremost requirement of Linux administration, you need to be able to login to remote system to do any kind of task on it. On windows putty or similar software can be used. On Linux or Mac OS machines you can directly use ssh command to do this. Telnet was once used for this purpose but nowadays it is outdated due to security issues. You can also make password less connection to your servers from your machines to automate many tasks. Read more about how to setup password less connections.

2. File Operations
You must be able to create,copy and move files and directories around your file system. Many people who are used to work on windows CMD prompt or Mac and Linux’s terminal will not face any problem in this. You just need to careful about using commands while doing operations which impacts files or directory life. Like using rm with rf option  with a small spelling mistake can erase your whole data without confirming.

3. Apache or your web server configuration
You must be aware of your web server’s functioning and its common configurations. Like on Apache you must be aware of virtual host settings and .htaccess file handling. You must be able to differentiate between ‘Dos’ and ‘Donts’. I can give you a point to ponder about. How using .htaccess file can slow down your overall file serving or execution via Apache and how you can overcome this? Try to found out information about it.You can also guide Linux Administrator about disabling modules which are not needed in your application to minimize memory foot print of the web server to boost overall performance.

4. Configurations
You must be able to see how your server is performing and how you can fine tune simple things like allocating more memory to your web or application server. If running PHP you must be able to handle common configuration of it as all configuration variables are not configurable through ini_set function or .htaccess file. Optimizing your database configuration can also be one of the useful thing to do.

5. Rsync
This is Swiss army knife for syncing your data between different servers. It only copy changed files and compression can also be used on the fly. You can start playing with it on your local machine and when you think that you can handle it, you will be surprised to see the impact it will make on your deployment process.

6. Using SVN, Git from command line.

You must be able to handle common operations related to SVN or Git through command line. This will help you to integrate your deployment process to your version system.

7. Using Cron Jobs

You must be able to set the schedules of your cron jobs. Using cronjobs you can handle routine work like clearing garbage or regenerating cache of specific portion of application.


I have tried to enlist some common tasks groups which you need to know to be better at linux administration as web developer. Some very common tasks like creating or editing files in ssh shell are also necessary in some cases.

How to identify Bigdata? A first hand explaination

Bigdata is the word which is being used very often but yet to be defined properly. How much data can be classified as big data? Does big data only means big in size or big in complexity or both? If hard disks can be clubbed together to store very very large amount of data then why whole world has become so obsessed with big data? Is it something different from traditional DBMS?

There are many questions which may come to the mind of a person who is starting his journey with big data. I can recall one of my friend Amrit who worked with a company which was also selling computers in year 1999. I asked him for quotes and he told me about a computer which was having 2GB of hard disk. He was very excited, in excitement he declare that you will not be able to fill that hard disk in next few years. In his words, “Its really Big hard disk which can hold big data” Today we can only laugh on it. Today even my car keys have a 64GB flash drive companion.

Cartoon: Big Data

Big is a relative term and its quite subjective. Although nowadays when data runs in several hundred GB we start calling it big data. My first encounter with big data was with apache logs on a server which was hosting more than few thousands websites. Due to restrictions on the number of opened file pointers on server, I tweaked apache configuration to store all logs in a single file with virtual host information as first column of the records.

We were supposed to process those logs for awstats log analyzer. Server was already under heavy load, so we transfer log files to another server and run our processing routines there. After processing data we were putting furnished awstats files back to originating server so users can see site stats without going to another server. For us this was big data.

I can identify following things due to which it was big for us :-

1. A single file was needed for multiple loggers as we were not able to write to multiple files at once due to restrictions on system.

2. As server was overloaded with high number of requests, we were not able to process our large file which require high memory as well as processor time on the same server.

So data can be classified as big data if your one machine is not able to create, hold or process it for the purpose you want to achieve.

If your machine is not sufficient to process or hold your data then first thing which come to your mind will be an upgrade of hardware. There is a catch in this option,  we have limitations on hardware upgrades and there will be a time when upgrades will not be possible.

So you need multiple systems to act like one. Whenever you feel that you are in that situation, you have big data at your disposal to handle.

Categories: PHP Notes Tags: ,

6 Algorithms you must know to be a good programmer

Algorithms are defined sets of instructions to solve a problem or achieve something. Almost every process in computer world can be classified as an algorithm, still it is most difficult part of computer science for many learners.

Algorithms are necessary to make your programs intelligent and efficient. They can make your program rock and it can stand out in front of competition. You need to be good at analysis, understanding and draw abstraction to get good hold on this subject.

In this post I am trying to compile a list of algorithms which every program should know. This list is not exhaustive and only indicative of some important sections of computer programming.


Visualization of sorting algorithms experiments
You must be good at different kind of sorting algorithms like quick sort, bubble sort, merge sort, heap sort and many other useful sorting techniques. You must be able to differentiate between these algorithms based on their features, problems and potential usage scenarios. Try your hand with different combination of algorithms, who knows you can discover a new optimal algorithm while doing so.

Binary Search

binary search tree
This algorithm is necessary to be in sync with higher efficiency demand  which big data is presenting in front of programmers. Many databases are using this Binary search algorithm to provide quick query answers and it is also useful in many other scenarios.

Graph Search

Twitter social graph (social dances)

In this social network era, this algorithm is one of most needed and used algorithm. Many Algorithm like shortest path etc falls under this category. This category of algorithm is usually considered most difficult one to understand as it involves lots of dynamic variables. Knowledge of calculus in math really help in understanding this subject. Once you got the idea behind graphs, you can rock those optimal solution problems.

Stable Marriage

Do you know computer science can help you to find a good life partner or friend? This algorithm try to solve this problem in a given scenario where priorities of available people are dynamics and not linear in nature. This algorithm usually works to suggest good friends and life partner on may social networking or dating websites.

Map Reduce


Many algorithms has been developed to deploy MapReduce which is essential in developing distributed applications handling large data. This is essential part of Hadoop system using which we can run parallel processes to process a large amount of data.

Hilltop Algorithm

Many search engine marketers knows this algorithm which was initiated by Google to rank pages based on some kind of reference values. This algorithm (or similar algorithms) is necessary to dive into large amount of data which our world is generating everyday. Now we are facing a hill task to find useful data or pattern from so much of data noise, hill top can help in this.


There are many hundreds algorithms but you can start with these algorithms before you go to your journey of mastering this interesting subject.


One major thing which needs to be understood regarding algorithms are their patents. Many algorithms are patented and can not be used in our programs without a license.  So be sure that you are using an algorithm which you are entitled to use in your commercial programs for distributions. Many algorithms has been released under open source licenses.

15 Questions about Magento Ecommerce System

I am enlisting 15 questions related to Magento ecommerce system. If you can answer these questions, you can consider yourself as a good beginner in Magento.

To understand Magento you should cover at least following areas :

  • Store Management
  • Product Management
  • Category Management
  • Inventory Management
  • Understanding of product variables
  • Layout and themes
  • Cache Management
  • Backup and Restoration of Backup
  • Extension Overview

1. What is Magento and how it is different from other e-commerce solutions available in the market?
2. How to create new Magento extensions?
3. How to override Magento functions?
4. How you can change the layout of a site?
5. What is store concept in Magento? Can we launch multiple stores from same domain?
6. How we can increased the speed of a Magento application?
7. What is Magento cache? How we can control it?
8. Can we setup out own cache engine? How?
9. How to change the Magento admin password if you forgot it?
10. What steps you will take to upload a Magento site to a different domain?
11. Examples of some popular extensions? How they can be installed in Magento?
12. What are hardware requirements for Magento?
13. How you can create multiple thumbnails of product or item images?
14. Can you integrate a new payment gateway to Magento? How?
15. Can you change the flow of checkout? How?

How to optimize all databases and tabels on mysql server?

Optimization of database tables in mysql is a process which has to be carried out regularly to make your database in good health.

According to mysql manual,

OPTIMIZE TABLE should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows (tables that have VARCHAR, VARBINARY, BLOB, or TEXT columns). Deleted rows are maintained in a linked list and subsequent INSERT operations reuse old row positions. You can use OPTIMIZE TABLE to reclaim the unused space and to defragment the data file. After extensive changes to a table, this statement may also improve performance of statements that use the table, sometimes significantly.

In phpmyadmin you can see data similar to following screen if your database tables are not optimized. Entries in Overhead column indicates size of data space which can be reclaimed after optimization.

A major challenge comes in optimization when you have many databasesm thus many tables on my mysql server. It is hard to do optimization manually for each and every table. So we have created this script which can optimize each and every table of each database on your mysql server. You can run this script as your cron script also so you can do your optimization activities automatically. We have also executed repair statement which will repair any table error.

$db_list = mysql_list_dbs($con); $dbtoProcess=array();
while ($row = mysql_fetch_object($db_list))

foreach ($dbtoProcess as $dbname) {
$sql = "SHOW TABLES FROM `$dbname`";
$result = mysql_query($sql);

while ($row = mysql_fetch_row($result)) {

foreach($AllTablesinDB as $value)
mysql_query("optimize table $value");
mysql_query("repair table $value");
//print $value ." has been optimized and repaired";


Execution of this script may take some time depending on the size of databases and tables.

How to integrate Magento and Drupal?

Magento is one of popular and robust e-commerce system which is based on the Zend Framework by Zend. It gives good tools and mechanism to operate your e-commerce websites and even multiple stores on different urls via single admin section.

Drupal is one of the best open source CMS having power of ease of use and lots of available modules which helps to build new functionality for users. It has powerful cck module for managing different meta values of a content entity. View helps to give representation of data in drag and drop kind of interface.

Now Magento and Drupal can be integrated with each other thanks to open source and free modules/extension which are available.


Drupal extension for Magento

Magento provides and extension for integrating Drupal into magento.

As per details given by the developer of Drupal extension :-

Drupal extension is a set of Core API extensions that offers a full access for Drupal modules  to Orders, Catalog and so on. This extension, will offer you to TOTALLY bypass Magento front office, using Drupal instead. When a new user is created a Magento user is automatically created also. Orders can be created from Drupal, cart is managed from Drupal. Catalog is syncrhonized, so each Magento Product is a Drupal node that you can push into Views, etc.


Magento module for Drupal

Drupal provides a Magento module which helps to import many magento entities in drupal as cck and you can also use views to customize your presentation layer.


As per details given by the development team at Drupal :-

  • Synchronization of Magento products to Drupal (currently supported type of products are: simple products, configurable products). All Magento attributes are dynamically synchronized into Drupal CCK fields
  • Custom product options
  • Up-sells, cross-sells, linked products
  • Synchronization of Magento categories into Drupal taxonomies
  • Synchronization of Drupal users to Magento customers, synchronization of addresses
  • Synchronization of currencies and currency conversion rates
  • Stock management
  • Full-featured shopping cart
  • Fully themable and customizable checkout process
  • Coupon codes
  • Payment modes API
  • Shipping methods API (support of all simple Magento shipping modes works out of the box)