How to Round to the Nearest 5 Cents (Nickel) With PHP

rounding with php

I was working on a pricing calculator in PHP and I needed to round the total to the nearest five cents, since my country got rid of pennies some years ago.

For example:

$2.23 should get rounded to $2.25

$2.21 should round to $2.20

$2.28 should round to $2.30

and so on…

Oddly enough, I didn’t find a whole lot of answers online explaining how to solve this seemingly simple problem.

So I decided to post my solution here in case someone comes across the same problem.

First we store the cost in the variable:

$cost = 27.2345567;

Then we pass the cost divided by 0.05 into the round function and multiply it by 0.05. This gives us our rounded value which we can store in a variable we’ll nameround_num.

$round_num = round($cost / 0.05) * 0.05;

That will round whatever number you assign to the cost variable to the nearest 5 cents.

This won’t display zero values, so 2.20 will be displayed as 2.2 and 2.00 will be displayed a 2.

To fix this, and make our numbers display consistently, we can use PHP’s number_format function like so:

number_format($round_num, 2);

The number_format function takes two arguments separated by a comma. The first argument is the number we want to format, which is our rounded number. The second argument is the number of decimal places we want to show. In this case we say 2 because that is the convention for pricing.

Here is the whole script, where we echo out the final cost at the end:

$cost = 27.2345567;
$round_num = round($cost / 0.05) * 0.05;
echo number_format($round_num, 2);

I am sure there is a more elegant way to do this, and by all means, post it in the comments.

But it works, and is pretty simple. So if you are like me and trying to solve this problem, maybe you will find this useful.

Top 5 Free Stock Photo Libraries

camera

One of the first things I learned as a web developer is that you can’t simply take any photo you find on Google images and use it on your website. No, most high quality photos are taken by professionals and they need to be paid to keep making awesome images.

There are lots of great sites to buy stock images, and you can find photos of practically anything you want. But there are many cases where it’s just not realistic to pay for photos. Maybe you just need to spice up your blog with a few photos, or want a nice banner for your Twitter profile. Or maybe you’re building a simple web page for a non-profit and they have zero budget.

Whatever your reason, I’ve compiled this list of 5 awesome free stock photo libraries. Note that while they are all free, some have certain restrictions on how you can use the images, but I will cover this which each site.

1. pixabay

pixabay logo

This is the first free stock image site I came across when I was first learning web development and always needed images for the test sites I would build. Boasting a collection of “over 570,000 free photos, vectors and art illustrations”, pixabay is one of the largest libraries of free stock images.

The quality of the images range from professional to cheesy 90’s clip art. So while more doesn’t always equal better, there is definitely enough good content on here to suit most of your needs. Sometimes you’ll find the perfect image on here only to realize it’s not the right ratio or quality you need, but nothing that’s free is ever going to be perfect. They also have stock video footage which is pretty cool to play around with in your designs.

The site design is attractive, modern and easy to use. All photos are 100% free to download, use and remix. You don’t need to give attribution either, but I’ll often do it anyways just to show the photographer and website some love.

2. Death to Stock

death to stock logo

This is one of the more interesting stock photo services. Death to the Stock Photo is a subscription based service. Just sign up with your e-mail and every month they send you free high quality photo packs. There is usually a theme to each pack, and its intended as a way to inspire other creatives. They have a nice explanation of it on their site.

This obviously isn’t great if you are looking for a very specific photo, but the photos are very professional and well done, so it’s a good one to have in your back pocket. I usually just download them into a folder on my computer and build my own stock photo library. It’s great to browse through every now and then when I’m looking for design inspirations.

You are allowed to use the photos however you want, with the usual restrictions: like you can’t use them to promote hate speech or on a pornography site, etc.

3. Wikimedia Commons

wiki media logo

Wikimedia Commons is essentially the media database for all of Wikipedia and their projects, but it’s also free for you to use. It boasts over 30 million media files including videos, sound recordings and photos.

It’s not the best option if you need sleek stock photos for your site, but it’s an amazing resource when you need something very specific. The site has a bland design that’s consistent with Wikipedia, and its usability suffers just slightly because of this, but if you are reading this article you’ll have no problem using it.

While all of the images are free to use, by what terms depends on the particular image. Most images just require you to give attribution which it specifies fairly clearly how to do so on the image’s page. Some may have different requirements, so you just have to check what is specified for each image.

4. flickr

flickr logo

flickr is one of the most popular photo sharing sites in the world. Photos range from someone’s wedding photos to professional nature photos. Any user can upload their own photos of whatever they want, so there are photos of everything. It’s more of a social network and sharing platform than a stock photo library, but you can find some amazing stuff on here.

Not all the photos are licensed to be used by anyone, so you have to go to their creative commons section. There, you can browse all the images that photographers have submitted under a creative commons license. Again, a lot of it is just random peoples photos, but I’ve also found some really amazing one’s on there, so it is definitely worth checking out.

flickr also has a section of their site called The Commons, which is different. Here they have partnered with archivists to build a collection of public photos. It’s mostly historical and vintage photos, but they are cool to look at and perfect if that’s what you need on your site. It appears that they are all public domain images so they have no copyright restrictions, but always check on the image’s page to be sure.

flickr is not my favourite on this list. Because its main function isn’t for free stock imagery, it’s not the best resource for it. I find the navigation, search function and browsing to be a bit of a poor experience on the site, and sometimes you find the perfect image only to find it doesn’t have the type of license that will allow you to use it. But at the same time I have found a lot of gems that were perfect for the project I was working on. So while it’s not my number one choice, it’s always a good last resort.

5. Pexels

pexels logo

I’ve saved what I feel is the best for last here. Pexels is a free high quality stock photo library. All the images are suitable for the most professional websites and designs. While it doesn’t have as many images as pixabay, it only has high quality images, so you don’t need to browse through a bunch of mediocre images to find the gems.

The interface is clean and a joy to use. You can download the images at whatever resolution you want, and everything is free to use without attribution for any legal purposes. You can sign up for a free account, which allows you to favourite images you want to use later. This is the first site I go to when trying to find free stock images for a new project. Often I will just browse through their images to get inspiration.

Recently they have added stock videos. Again, while the selection is a bit lacking, they have some beautiful videos for you to use on your site.

Conclusion.

Finding the right image for your website can be tricky when you have no budget, but with these sites on hand you can be sure to find something to suit your needs. They all have their strengths and weaknesses, so it’s best to use them all in conjunction.

Always make sure you read the licensing agreement on each site to be sure you aren’t violating their copyright in anyway. Each site does a good job of specifying this pretty clearly, so it’s very simple. Also, even if a site doesn’t require you to give any attribution, it’s still nice to link back to the site or photographer in some way to give them some attention. You can do this with an image caption, credits at the bottom of your page or blog post, or even as an HTML comment for other developers to see.

There are other free stock photo sites, but these are the top 5 that I use as a web developer. Let me know in the comments if you have any other suggestions.

It’s Time to Update Your Browser

Logos for the major internet browsers: Internet Explorer, Firefox, Safari and Google Chrome

As a student of web development and a person who believes the web is first and foremost a platform for sharing knowledge, accessibility is huge. When I create a website or application, it’s important to make sure the content is accessible no matter the device the person is using, the speed of their internet connection, or their ability.

For the most part this is easily accomplished through applying best practices and conforming to the latest web standards when writing my code. Among other things this includes using alt tags on things like images and Aria Roles to specify sections of content to assist blind users. For rural users, I also like to optimize images and other media to load faster while still looking nice (unpixelated). And of course, it’s important to make sites responsive so the experience is consistent whether the user is on mobile, tablet or desktop.

This only requires a few more snippets of code here and there, but the results are massively beneficial. Not only does it make my site or application more accessible to all users, but it also ensures my work is optimized for search engines. Together, this ensures my content is viewed, or at least able to be viewed, by as many people as possible; both because it is accessible and search engine friendly. It’s a win-win.

However, there is one other thing to increase accessibility but I hate doing: including support for older browsers. The newest versions of HTML, CSS and Javascript come jam-packed with tons of cool features which make things easier and more enjoyable for both the developer and user. Yet, if the user is using an outdated browser, it often requires several workarounds to get these functionalities and features working properly for them.

It’s not that I’m lazy, but that there is no reason not to have a modern browser installed on your computer. In fact, there is every reason to ensure you are using the latest version of any of the amazing browser offerings available:

  1. It’s free! All of the latest and greatest web browsers are 100% unequivocally free. That is pretty fantastic for what is probably the most used application on the majority of users’ computers.
  2. It’s secure! The newest browsers include better protection against malware and other potential threats on the web, as well as fixing bugs and other security threats in older versions.
  3. It works! Not all developers are nice enough to account for older browsers. Having the latest version of a browser will ensure you experience the full functionality of any website you visit.
  4. It has awesome features! The newest browsers come with a lot of great features which make your browsing experience more easy, enjoyable and efficient. There are also a ton of free add-ons available that can do things like block annoying adds or organize your bookmarks.

Now, if you are one of the people still using an old browser, you might be wondering, “okay you’ve convinced me, but which browser should I install?” The simple answer is: any and all of them (well, at least any of the major ones). Seriously, there was once a time when one browser dominated all the others in terms of compatibility and performance, but now a day they are all more or less equal for most users’ needs. You can simply go here to check what current browser you are using and select one of the latest and greatest browsers to install. I recommend installing a couple, try them out and decide for yourself which one you like the best.

If you already have a modern browser installed, I encourage you to share a link to this post with everyone you know, check your friends or family member’s computer and make sure they have one of the latest browsers installed.

Together, we can build a safer and more enjoyable web experience for everyone!