+44 (791) 5698 086

KISS code

I’ve seen numerous people, doing simple things in amazingly complex way!

As they say, programming is not typing, it’s thinking. Maybe it’s hard to think for a few minutes instead of coding for many more minutes over the same thing, in a bad way?

In this article, I’ll keep adding some KISS (Keep It Simple Stupid) code examples.

How to remove trailing Zeros

There are conditions where your string might have trailing zeroes and you might want to get rid of them.

A google search resulted in some amazingly (and stupidly) complex methods that people were using to achieve it. In fact some proposed to use regex even! Wow! It’s true that when you have a hammer in your hand, everything seems to be a nail.

Anyway, to remove the trailing zeroes before the string or number that you’re using, do this simply:

 $a = '000000000000001';
 $a += 0;
 
 echo $a; // will output 1

How to sort an array of Objects by a field

This function will convert your array of objects into a new one with the sorting of your choice. (You can use it with vanilla php or wordpress too, if sorting isn’t working due to any other plugin or theme code, e.g. on get_categories() or get_terms().

function cmp($a, $b){
    return strcmp($a->name, $b->name);
}
usort($your_array, "cmp");

Let me know if you have any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *

Who are we?

Nabtron team includes web, desktop software and mobile apps developers along with SEO professionals.

We are available 24 hours, 7 days a week. Contact us now for anything you need!

Join our mailing list

Lets become friend :) What's your name & email?