Monday, March 24, 2014

The Idiot PHP Developer's Creed

I am a PHP Developer and this is my creed.

PHP is the best programming language, ever. 
I don't need no framework or library, my native code is light and fast.
I can code anywhere, as long as it works, it is correct.
I don't need OOP,  best practices, design patterns, software architecture and all that crap. They slow me down.
I rate myself 9 out of 10 because I memorised most of the native functions or pretend to be.
I rate myself 10 out of 10 because I can build a shopping cart website within 3 days or because of whatever. 
I prefer the use of arrays because they are better than objects.
I believe project managers, business analysts and QA are useless.
I don't have to test my work, somebody else should.
I don't have to look at the error logs, my code is perfect.
I copy-paste code that the first page of my Google search results which seems cool and working.
I don't need to understand what I copy-pasted.
I claim the copy-pasted code as my own and grin with pride.
If my code CANNOT be understood, then I am super awesome.
The hardest thing I did is to build a website using WordPress.
I don't need Math, Statistics, Science, or Business to level up in my field. I can do anything with PHP.
I develop PHP using WAMP and use a Windows-based IDE while production deployment is in Linux/Unix.
I use get_browser() function to detect user agent.
I use static methods, globals, constants every chance I get. 
I only need if-then-else to implement logic and algorithm. The longer, the better. I love spaghetti.
I loop through an external resource call.
It takes an FTP uploader to deploy my code.
I have hardcoded stuff everywhere.
I don't need to document my code or put doc blocks for that matter. It takes away precious development time.

(to be continued)

Tuesday, March 18, 2014

Kill Sleeping MySQL Processes using Quick PHP Script

Our server was getting swamped with too many sleeping connections causing "too many connections" error. During production hours, manually killing them isn't an option, there are just too many and MySQL nor MariaDB doesn't have a native tool for it. I needed it fast, so I wrote a quick PHP code which can then be run every minute as a CRON job.

Here's the code:

mysql_connect('yourhost', 'username', 'password');
$res = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($res)) {
  if ($row['Command']=='Sleep') {
      if ($row["Time"] > 3 ) { //any sleeping process more than 3 secs
         $sql="KILL $pid";
         echo "\n$sql"; //added for log file

You can then save this to a file mysqlkillsleeping.php, and add it as a CRON job running every minute

vim /etc/crontab

*/1 * * * * root php /path/of/mysqlkillsleeping.php 2>&1 >> /var/log/mysqlsleep.log

Thursday, February 13, 2014

Yum Install ImageMagick

ImageMagick is an image manipulation application which supports most of the standard image formats.  It is also a very powerful command line tool for batch processing images. If you wish to use it in CentOS or Fedora, just install it using YUM package manager.

# yum install ImageMagick

How to Add 3rd Party Libraries to Zend Framework 2

You can easily add other libraries to Zend Framework 2 whether they are your custom libraries, 3rd party libraries or standard packages from or Github.  However, the explanation and method on how to add and autoload them properly to the software architecture was not easy find, at least for me.

Since you're here, I've made it a tad easier for you and itemised the use-cases.

1) Adding custom libraries which are GLOBAL to your application 

 Edit composer.json and add within the closure,

    "autoload": {
        "psr-0":  {"MyLibrary\\":"vendor/my/library/path"}

Then run "composer update" from the CLI under your application root directory. This will add an entry to your <APPLICATION_PATH>/vendor/composer/autoload_namespaces.php.

Alternatively, you can directly add an entry to it.

return array(
    'Zend\\' => array($vendorDir . '/zendframework/zendframework/library'),
    'MyLibrary\\' => array($vendorDir . '/my/library/path'),

Monday, January 27, 2014

How to Make Your Module Default in Zend Framework 2

Just make sure you have the proper route to slash (/) and the first in the alphabetical hierarchy of the other modules.


'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/', 
                    'defaults' => array(
                        'controller' => 'Account\Controller\Index',
                        'action'     => 'index',

Friday, January 24, 2014

How To Set-up ZendFramework 2 for Developers from Scratch

Here are a few easy steps to bootstrap your development using Zend Framework 2.x. This assumes that you are using PHP 5.3.x or higher on a *nix machine. Was tested to work on Fedora and CentOS.

1) Install composer

(make sure you are a root user)
# sudo -i
# curl -sS | php -- --install-dir=/usr/local/src/
# mv /usr/local/src/composer.phar /usr/local/bin/composer

# chmod +x /usr/local/bin/composer

2) Download and install ZendFramework Skeletion Application

# composer create-project --repository-url="" zendframework/skeleton-application /path/to/install/skeleton/app dev-master

Wednesday, December 18, 2013

PHP: Detect Credit Card Type and Simplify Payment Form

Simplifying forms is the way to go to improve the payment experience. It's pretty much becoming the standard to not ask the credit card type anymore.

Here's a clean way to do it in PHP 5.4+.
Note: To work with PHP 5.3 below, change square brackets with "array()".

function getCreditCardType($str, $format = 'string')
        if (empty($str)) {
            return false;

        $matchingPatterns = [
            'visa' => '/^4[0-9]{12}(?:[0-9]{3})?$/',
            'mastercard' => '/^5[1-5][0-9]{14}$/',
            'amex' => '/^3[47][0-9]{13}$/',
            'diners' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
            'discover' => '/^6(?:011|5[0-9]{2})[0-9]{12}$/',
            'jcb' => '/^(?:2131|1800|35\d{3})\d{11}$/',
            'any' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/'

        $ctr = 1;
        foreach ($matchingPatterns as $key=>$pattern) {
            if (preg_match($pattern, $str)) {
                return $format == 'string' ? $key : $ctr;

You can further enhance the experience on the UI part by adding a javascript equivalent of the above function and have it dynamically change credit card logos indicating credit card type dynamically as people type their credit card number.

This implementation was based on an answer found here. Fyi, Google, Github etc are using the same method for their payment forms and a lot more are doing so as the day pass by to improve their conversions.