Monday, December 14, 2015

Chunk IPv4 range into multiple sub-chunks

One of my non-programmer-but-in-IT friends came to me last week, asking help to chunk a range of IPv4 addresses into N small chunks. And, here's the PHP code I came up with within 10 mins.

What it simply does is, explode the input range, and convert start and end IP address into long, using ip2long(). Then, using range() expand it into a whole list. Once that is done, then decide number of elements that should go into each chunk - remember, the optional argument to this function tells how many chunks are to be prepared. the formula is there on code line number 11.

Once you have this number, call array_chunk() to create chunks. Finally, iterate over them, get start and end IP of each of them (which are still long numbers), convert them back to IPv4 address using long2ip() and create the range string for them.

Do you find it useful? Copy it! Have any suggestion, let me know in the comment!!

Wednesday, May 13, 2015

Ordinal Number

Here is a PHP function to get the ordinal number. Pass it a number, and it will return a textual number suffixed with either of "st" (like 1st), "nd" (like 2nd), "rd" (like 3rd), "th" (like 4th) etc.

Grep - before and after

The grep is a command line utility for searching plain-text data sets for lines matching the given regular expression. And those using Linux (or some or other flavor of this OS), must have used this command. Here are two useful options of this command: -A for After and -B for Before. Both take a number as their value.

As their names suggest, -A gives you matching line and N lines after the matching line, similarly, -B gives you matching line and N lines before the matching line.

You can also combine both these options into single command. And, if your -A and -B values are same, then you can simply use -C option. C stands for Count.

Wednesday, May 6, 2015

PHP Blunder-2

Here is another 'oops!' moment for PHP. Do you remember the rule of transitivity? A related to B, and A related to C, implies that B is related to C. Right? Now apply this very understanding to the code below.
Now, foo equals true, and foo equals zero. So, according to this rule of transitivity, true equals zero?!

Wednesday, April 29, 2015

PHP Blunder - 1

PHP is one of those most popular web programming languages, but it do have some serious mistakes. Here's one of them.

PHP have a predefined value 'null'. And, it can be compared with numeric values. As anybody else would, you would expect null to have a constant value, right? But that's where PHP have a problem. Look at the code below, and execute it on your system if you want to check.

The null == 0 evaluates to true. So does null < -1. How can some predefined value be equal to 0 and lesser than -1 at the same time?!

Tuesday, April 28, 2015

Graceful Degradation and Progressive Enhancement

These two terms are of pretty much importance when it comes to developing web apps or sites. Both, graceful degradation and progressive enhancement consider how the web site or app works when the browsers and devices vary.But the main difference is between where they focus.

Graceful degradation is the practice of building your web functionality such that it provides a certain level of rich user experience on modern browsers and devices, but it will also degrade gracefully to a working lower level on older browsers and devices. This working lower level is of course not a nice-to-have for your app/site users, but it still provides basic functionality so that things won't break for them.

Progressive enhancement is quite similar, but it goes the other way around. You start by establishing a basic level of user experience that all browsers/devices will be able to provide when rendering your web page, but you also build more advanced functionality that will be automatically be available to browsers/devices that can use it.

In simple and short words, graceful degradation is starting with higher experience quality and trying to fix it for lesser experience; whereas, progressive enhancement is starting with basic, working functionality and allowing for extension for future environments.

Degrading gracefully means looking back where as enhancing progressively means looking forward while standing on firm ground.

Example of Graceful Degradation v/s Progressive Enhancement
Let's take a simple example of "Print This" link. As such, links that allow users to print the page are useless because the user can do the same by using browser's 'Print' button. Perhaps, letting users trigger this from within the page gives a feel of having completed the process by themselves. Think of ticket booking system to visualize this, where you yourself hit the print link or icon when you are done with all the formalities to book the ticket.

You need Javascript for that. The window object of the browser have a print() method available, which you would call and trigger the print dialog. But, what would happen when Javascript is disabled on the browser? Your code will simply not work! And, that would create very bad impression about your page because you had promised that functionality!

With graceful degradation, you would take these steps to handle this:
1) Add a link to print the page.
2) Add <noscript> tag, which shows its contents only when Javascript is not available, and use it to suggest some alternative to print this page. Something like "Printing this page requires Javascript enabled. Either turn it ON, or use File > Print menu."

But this also requires your user to know what Javascript is, and how to enable it. Also, we are assuming that Javascript is available.

On the other hand, with progressive enhancement, you would take following steps to handle the same situation;
1) Show a simple message like "Please take a print of this page to keep on your records". Use the id attribute to identify this message container.
2) Using Javascript, check if type of window.print is 'function'. And, if it is so, create a button element having 'Print Now' value and append it to the above message.

See how defensive this code will be. Here, we aren't assuming anything.This will work for every user regardless of how the environment is.

It can be said that both - graceful degradation and progressive enhancement - try to do the same thing: keep our web site/app useful to every user. Progressive enhancement is more sophisticated and at the same time a stable way of assuring that, but it also takes more time and efforts. Graceful degradation can be used more easily as a patch for already existing page, but it also means harder maintenance later on, but requires lesser initial work.

Monday, April 27, 2015

Saturday, April 25, 2015

Week of the Month

In my project work, I needed to find out whether the given date falls in 1st, 2nd, 3rd, 4th or 5th week of the given month. As usual, I googled for this solution and found many functions. http://www.hotscripts.com/forums/php/37900-week-number-month.html, http://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month and http://mybroadband.co.za/vb/showthread.php/66311-Calculating-the-week-in-the-month-(PHP) are some of them. But, being lazy, I thought those functions were too complex, and there could be a simpler solution to this. So I spent some time and came up with this function. Here is the code.

Hello World!

Hello World! 

Here with this blog post, I get started with my programming blog!! It's a well known fact that learning each programming language starts with a "hello world" program. And, so I wanted to start my programming blog with a "hello world" post. :) 

Oh, what I would be blogging about? Well... I haven't decided any fix theme for this blog. And, neither I want to tie it to any particular blogging genre. So, I would definitely like to make it a mixture of how-to's, code-sharing, tips, tricks, information sharing and what not?! Most of the times I work in PHP, so, yes, most of the blog posts would be related to PHP and web-world. Apart from that, I would be blogging about web UI frameworks, web-based apps, APIs etc.

Most of the code sharing will be through gists. I am a strong believer and follower of social coding, and that's why I keep on storing (and maintaining) my code snippets on gist/github. Feel free to follow me there as well. ;)

I hope I get plenty of time and information to share here!