Why don’t I use array iterators in PHP?

Art VS Performance

When I started learning PHP, most beautiful thing which I could do is to use array_walk, array_map and array_filter functions along with some closure which will attracts some cheers. Today I told my team that they should remove it. But why? The question was raised instantaneously. I said, do the benchmark and you will know the answer.

PHP Functions are overkill! Which should be the part of third party libraries are part of core libraries. Unfortunately, our interview selections make us learn the unwanted core functions. Two years experience favorite question would be different between them! Give it another year and you would know why for loops are great!

Arguably – Performance isn’t much! Check few results over here but then it also depends on how much traffic are you handling. This demands a read on an article on Freaks – Sanity in Insanity I wrote. The overall results from above read is as following:

    //1M numbers across a dozen attempts
    Foreach: 0.7 sec
    Map on closure: 3.4 sec
    Map on function name: 1.2 sec.

But then – Readability!

The functions – array_walk, array_map and array_filter kill the readability all together. It might reduce the number of lines you are reading but – and it is a big but – the loops are more familiar as well as keeps thing in indentation.

Worth Sharing?

How to install Imagick in PHP Docker?

The PHP docker image is build from the source and hence the usual apt-get install php-imagick doesn’t work at all. This might be frustrating for you as most of the solutions have a version attached to it. However, I found a way to always have it updated. Here is how –

Dockerfile for Imagick

This is how docker file should look like:

FROM php:7.3-apache
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick

Build Output

....

configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini
Removing intermediate container 1c032fba6d45
 ---> 517795f672ac
Step 4/4 : RUN docker-php-ext-enable imagick
 ---> Running in 976ecd4a9627
Removing intermediate container 976ecd4a9627
 ---> 4b978dd49756
Successfully built 4b978dd49756

Git Repositor and Docker

Worth Sharing?

How to write good “If” statements?

Writing a good code is an art and it can be useful in so trivial things that most developers often ignore. Usually, amateurs are programmed to write an ‘if’ statement which is always accompanied by an ‘else’ . However, it is important to realize that code quality can significantly be improved if else is omitted. For instance, consider this code.


function getUser($id){
  if($id < 1){
   $user = [];
  }else{
   //some big codes
  }
  return $user
}

Here some big codes can be so big that it will return statement will be lost in it. Better would be:


function getUser($id){
  if($id < 1){
   return [];
  }
  //some big codes
  return $user
}

Worth Sharing?