Getting Query String Values Without a Hash in the URL

$ returns empty? Angular does not remove the query before the hash bang in URL. According to AngularJS documentation,  hash bang URL format is:!/bar?baz=23

So if you have no hash bang in your URL (e.g., and try to get query string value, then you can use this snippet.

function getQueryParams(queryParam, url) 
        var pageUrl = url ||;
        var urlVariables = pageUrl.split(/[&||?]/);
        var response = null;

        for (var i = 0; i < urlVariables.length; i += 1) {
            var param = urlVariables[i];
            var paramName = (param || '').split('=');

            if (paramName[0] === queryParam) {
                response = paramName[1];
        return response;
console.log(getQueryParams('baz', ''));

This code is also with pure JavaScript (withour AngularJS).

Start Counting After Scroll on Specific Element

Sometimes it’s important to do some activities when a specific element is visible on the web page. So something needs to be trigger when that element is showing on the web page. Suppose you want to start counting after scroll on that specific element. Here is the code for that.

<div class="container">
  <h2 class="well well-lg" style="margin-top: 200px;">Scroll Down</h2>
  <h2 class="well well-lg" style="margin-top: 200px;">Scroll Down</h2>
  <div class="jumbotron text-center" style="margin-top: 200px;">
    <div class="container">
      <div class="col-lg-3">
        <h2>Total Sales</h2>
        <h1 class="counter">967</h1>
      <div class="col-lg-3">
        <h2>Total Sales</h2>
        <h1 class="counter">967</h1>
      <div class="col-lg-3">
        <h2>Total Sales</h2>
        <h1 class="counter">967</h1>
      <div class="col-lg-3">
        <h2>Total Sales</h2>
        <h1 class="counter">967</h1>
function isCounterElementVisible($element) {
  var topView = $(window).scrollTop();
  var botView = topView + $(window).height();
  var topElement = $element.offset().top;
  var botElement = topElement + $element.height();
  return ((botElement <= botView) && (topElement >= topView));

$(window).scroll(function() {
  $(".counter").each(function() {
    isOnView = isCounterElementVisible($(this));
    if (isOnView && !$(this).hasClass('visibled')) {
      $(this).prop('Counter', 0).animate({
        Counter: $(this).text()
      }, {
        duration: 3000,
        easing: 'swing',
        step: function(now) {

See Demo:

Concurrency Issues With PHP Session

Concurrent connection means how many request sending/calling at the same time. It influences the application performances rapidly. When your application can handle as much as concurrent connections your application will become more stable, high scalable and faster as well. But concurrency might be a problem when you deal with PHP Session. It is the biggest challenges to avoiding concurrency issue in PHP, especially in PHP Session.

Most of the modern web browser sent concurrent connections to a specific domain to between 4 or 6. That means if your application has a lot of static assets (JS, CSS, Images, Fonts, etc), then these will be queued until browser get the response from the server for its first set of a request (4/6 concurrent request).

Concurrency Issues With PHP Session


By default, PHP uses files to storing the Session data. PHP either create a new Session or retrieve existing Session on each request when called session_start() function. So in this case, if your user sending multiple concurrent requests to the server which involves with Session data. PHP Session will lock and block your application’s concurrent request for that particular client. And every request will be served as sequentially instead of processing them concurrently.


For the new user, PHP sent the unique identifier number to users computer and create a new file with the same unique identifier name for handle the session for that particular user. Otherwise, PHP retrieves existing session for that user. See this article to know how does session works.

Now assume, a user sent 6 concurrent requests to server and 4 of them involved with session data. So what happened that case? PHP will take the first request and open the session file and lock it. And other 3 requests will be queued until session file will be released. So that means your application will be blocked. After your first request’s done, it will release the session file and the second request will take it. It’s will happen for rest of the request.

Now think every request taken 400-500 ms to execute, that means your last (4th) request completed after 1600 ms cause it waited 1200 ms to complete first 3 requests. So in this way PHP Session blocked the application.

However, this concurrent issue happened for the same user only. Request from a user cannot block another user’s request.

You will be able to understand how this thing can affect your application performance. To improve application performance every developer needs to avoid this issue.

Return Image as response by PHP

Return Image as response By PHP

To return an image as a request response by PHP you fpassthru function of PHP. I have included a sample code snippet, which will help you to understand how to do this.

From the above code snippet, you can see I have opened an image with binary mode by fopen($image, rb). And then send that with appropriate headers by header(“Content-Type: image/png”). After that dumping that image by using fpassthru, and finally stop the execution of PHP. It’s really easy to return image as response by PHP

Download and then Print PDF File in AngularJS

Sometime developer need to download PDF file and print that pdf document. Here I have shared a code snippet for download pdf then print that pdf using angularJS.

$scope.printPdf = function (uuid){
            url : 'PDF_URL',
            method : 'GET',
            headers : {
                'Content-type' : 'application/pdf'
            responseType : 'arraybuffer'
        }).success(function(data, status, headers, config) {
            var pdfFile = new Blob([ data ], {
                type : 'application/pdf'
            var pdfUrl = URL.createObjectURL(pdfFile);
            var printwWindow = $;
        }).error(function(data, status, headers, config) {
            alert('Sorry, something went wrong')
<button ng-click="printPdf()" type="button" class="btn btn-primary">Print PDF</button>

With this simple sample code snippet, you can easily download & print pdf by using angularJS.

Add Decimal Point Automatically After a Number

Here I have given a small code snipped for making a number as currency format by javascript only. This snippet will automatically be adding extra zeros after the decimal point. Let’s see it

Remove ngOptions Elements in AngularJS

With the passing time, AngularJS is getting popularity day by day. We all know AngularJS is the structured, most powerful and powerful front-end framework. But today I am not going to describe you about the AngularJS. But I am here to describe an easier & effective way to Remove ngOptions elements in AngularJS.

How to Remove ngOptions Elements in AngularJS

AngularJS provides a ng-options directive to display the options of selectlist. Here I use a categories array (array of object) for the selectlist. Every category object has (id, name, and is_active) keys and is_active true means Active category and is_active false means Inactive category.

Now, I want to display two selectlist, one if for active and another one for inactive. To do this I am using filter:{is_active: true} for active category’s selectlist and filter:{is_active: false} for inactive category’s selectlist.

With this filter, you can easily refine options base on your application needed. So developers can fell free to use is and make their code more maintainable and handy.

If you have any advice or query, please fell free to ask me.

What is Dependency Injection in PHP

What’s up, guys! Hers I am going to describe you the dependency Injection in PHP.

Dependency Injection in PHP:

Do you actually know what does it mean? Normally dependency injection means to inject something to another stuff. In programming concept, it means to inject an object to another class to perform an operation. In Software engineering dependency injection is really useful and handy design pattern. Programmers are widely using this design pattern to make their code more usable, testable and highly maintainable.

Programmers are always thinking how to manage their dependencies. They want to write good application code with avoiding dependencies. Here is the solution comes from design pattern concept to avoid the dependencies problem. I will use some code snippet to make you understand what is dependency Injection, why and how to use it.

Dependency Injection in PHP
Dependency Injection in PHP

Look at this code snippet:

At first glance of this code snippet, you might think there is nothing wrong with this code, For a moment I agree with you. But! if we have closer look on it then you can understand the problems of this code such as.

Here you can see the __construct of student class are creating an instance/object of course class. Which means we already written a hard-coded dependency for the student class. So the student and the course class are tightly coupled together. If we want to create instance/object of student class then we have to pass arguments for course class also. Cause student’s __construct is already creating instance/object of course class.

Now let’s imagine we made a change on course class. Suppose we add a new parameter in __construct method in course class. Then every class we need to change who created instance/object of course and they need to pass an extra parameter into course class.


Identifying The Problems:

Here you can see we added a new parameter into course class called lesson. And created a new class called department. So here student and department both classes needed course class and they are created instance/object for course class.

After adding a new parameter (lesson) in course class then student and department both classes are modified with passing a new parameter (lesson) to course class.

So every time you will need to do the same thing if the same case will happen. So if you have many classes like that, every class need to modify. And it’s a really annoying and error can occur easily. To avoid this types of scenario programmer are using dependency injection.


Look at this code snippet, here you can see in student and department both class __construct needed course instance/object as a parameter (2nd parameter). And is assign into ‘$course’ properties.

We created an instance/object of course class, and while we creating instance/object for student and department then using created course instance/object as arguments.

So that way if we made any class in course class, then we don’t need to modify our student and department class. We just need to change in  $course = new Course('English', 2, 5);'
(where the object is created).

With this strategy, you can widely inject your dependency to another class. And your code will more testable and maintainable.

Hope this article hely you to understand what is dependency injection in PHP and how to use it.


I believe you already heard about the PSR in PHP language. Today I am trying to help you to understand the what is PSR, how and why should we use it.


PSR knock as ‘PHP Standards Recommendation’ which is some recommendations for all PHP programmer when they write their code they should follow some rules and regulation. With following that recommendations anyone can understand other programmers code. It’s working like an eco-system or uniform system where we all following these systems. Basically, there are five PSR: PSR-0, PSR-1, PSR-2, PSR-3, PSR-4. So let’s see about these five.


Before we move forward we should need to know the history of PSR in PHP language. Honestly, there are no uniform coding standards for writing code for PHP programmer. Some programmer has written their code by with their own naming convention and coding style. Someone was following some framework’s conventions such as PEAR or Zend Framework. So there was a huge problem to understand other programmer’s code. Cause they do not follow any uniform rules and regulation.

With that passing time, FIG (Framework Interoperability Group) intends to introduce a cross-section of the PHP ecosystem, not exclusively framework developers. The main focus of FIG create a dialogue between project representatives, and with this concept, they were searching a suitable and useful way to work together and everyone comes under one ecosystem of PHP and they introduce some PHP Standards Recommendations (PSR-0, PSR-1, PSR-2, PSR-3, PSR-4).



PSR-0 refers to the ‘Autoloader Standard’. it comes with huge advantages to reusable the codes and loading the classes. Before the PSR-0 has come, we were working with PHP 5 magic function _autoload() which is automatically loading the class we required and gives us a flexible way to create the instance that class. But with  _autoload() you can able to provide only one autoloader function. Later PHP 5.12 introduced spl_autoload() which provides to register multiple autoloader function. Then PHP introduces the spl_autoload_register() function which gives us permission to create a series of the autoloading function. After PHP 5.3 PHP support true namespace and then the FIG (Framework Interoperability Group) written some guideline for the PSR-0. Below are the requirements for PSR-0 compliance:

  • A fully-qualified namespace and the classes must have the following conventions  <Vendor Name>(<Namespace>)*<ClassName>.
  • Each and Every namespace must have a top-level namespace (“Vendor Name”).
  • A namespace can have as many as sub-namespaces according to need.
  • Namespace separator converted into DIRECTORY_SEPARATOR.
  • Underscore in the class name converted into DIRECTORY_SEPARATOR.
  • Vendor names, namespaces, and class names might be of any combination of lowercase and uppercase.

These are some requirements for the PSR-0.



PSR-1 refers to ‘Basic Coding Standard’ for PHP programmers. Actually, PSR-1 focus on the naming convention, file structure etc. With these rules, the code is understandable to all programmers who follow the PSR-1 Rules. Below are the requirements for PSR-0.

  • Only allow to write <?php and ?> (<? and <? are not allowed) tags for PHP.
  • Only allow using UTF-8
  • Force to use PSR-0
  • Class name must be in PascalCase/StudlyCaps (that means follow Capitalization Conventions while write class name )
  • Class constants/global variable must be defined in UPPER_CASE with underscore separators.
  • Method names must be defined in camelCase.

These are some requirements for the PSR-1.



PSR-2 refers to ‘Coding Style Guide’ . Actually PSR-2is the extended and expands of PSR-1’s ‘Basic Coding Standard’. The main goal of the this Standard is moving all PHP programmer to follow a uniform coding guideline

  • The programmer must  follow a coding style guideline (PSR-1).
  • Code must have 4 space for indenting, tabs are not allowed.
  • Code should have the length limit (120 characters are known as soft limit and above 120 characters knock as hard limit. The code length limit should be 80 characters or less)
  • There MUST be one empty line after the namespace declaration, and there MUST be one blank line after the block of use declarations
  • Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility.
  • Control structure keywords MUST have one space after them; method and function calls MUST NOT.
  • Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
  • Opening parentheses for control structures MUST NOT have space after them, and closing parentheses for control structures MUST NOT have space before.

These are some requirements for the PSR-2.



PSR-3 refers to ‘Shared Logger Interface’ for PHP programmers. Which is for taking/storing the application/system log reports. When an application is running  then sometimes error, notice, alert, can be happened, in this situation we need to track/save these actions, and PSR-3 describe us how should we save these actions.

PSR-3 incorporating with eight Syslog levels of(debug, info, notice, warning, error, critical, alert and emergency), These Syslog levels describe as a function and it’s accept two parameters such as ‘Message’ and ‘Context’. Generally ‘Message’ is a string regarding with logs level and Context is for store additional information.

Recently monolog implemented PSR-3 so you can use monolog for storing your application log.



PSR-4 is the most recent added by FIG (Framework Interoperability Group). The PSR-4 is working together with PSR-0 where needed. PSR-4 is the replacement of PSR-0. The main goal of PSR-4 is to remove the remnants of PSR-0 and the pre-5.3 days completely and allow for a more concise folder structure. You can see the specifications of PSR-4 from here



The PHP-FIG does a great job of centralizing PHP standards. With this following these standards every PHP programmers will going forward with the same  concept. So every PHP programmer should follow these standards, so they can working together.






How to Create Custom Filter in Twig

Twig is the Fast, Secure, Flexible, and Powerful template engine of PHP. It has lot’s of great features for develope the front end. It’s has tons of built in filters, but sometimes we need to create our own custom filter and use them into Twig. With this article I am trying to describe you how to create custom filter in Twig.

I am showing an example so you can understand better. Go, where your Twig in loading paste this code.

Here, the custom filter name is ‘convert_object_to_array‘ Which is responsible for convert an object to array. Now you can call that filter in your twig file by {{ objectData | convert_object_to_array }}. Now you can type casting your object into array with this custom filter.