Converting a Decimal to a Fraction in PHP

Below is a helper function to convert a decimal, like for instance 6.5, into a fraction, 13/3 in this example. In fact the function will return an array with the number on the top of the fraction (the numerator) being the first item and the number at the bottom (the denominator) being the second item. Edit 25th Nov 2016: Changed the function to be more accurate and added some phpunit tests for it. The Function: /** * Convert a decimal … Continued

Create your own Exception Handler in PHP

If you’re used to writing your own PHP applications, you will no doubt be used to having to deal with exceptions being thrown at awkward moments. Resulting in a ugly 500 error being shown to the user and no extra direct from them to take. Luckily we can choose our own way of handling these exceptions, showing a better message or logging the error more efficiently. In an ideal world, we wouldn’t need these as everything would work perfectly, all … Continued

Create your own PHP Autoloader

If you’ve worked with any large file in PHP you will probably have come across a large block of include statements at the top of it. Includes allow one file to include many other files and are used a lot in OOP. A large amount of includes at the top your code poses two major annoyances: firstly they’re taking up valuable space which could otherwise be used for more important things; secondly because of their explicit nature, they get left … Continued

Smoother JS Animations with requestAnimationFrame

Within Javascript, if you ever worked on creating animations you’ve probably been dependent on the setTimeout() or setInterval() functions to do much of the work for you – incrementing a value at each execution and moving or changing an element. The situation in the code snippet below might look familiar, a function which contains a setTimeout() which changes a value, either continueously by calling itself or until a point by checking a condition with an if statement.

-0 === +0

I came across an interesting tweet from @getify the other day which mentioned the difference between -0 and +0 in JavaScript. This was news to me and it took a few moments to grasp the concept of both a positive and negative zero.