Page 7 of Software Development Articles

Sort an array in reverse order with PHP

Arrays are a great way to store / work with data. In PHP, arrays are one of the most powerful data types of available. From time to time, you need to sort an array in reverse order. Of course, PHP has you covered: $array = [ 1 => 'one', 2 […]

Pull random values from an array with PHP

Like most things, you can solve this problem with a number of different approaches. Without knowing about array_rand in PHP, you could end up doing something like this to pull a random value from an array: $array = ['one', 'two', 'three', 'four', 'five']; shuffle($array); echo $array[0]; The array is shuffled […]

Difference between dates with PHP

Prior to PHP 5.3 the act of calculating the difference between two dates was a bit more manual than it is now. By manual, I mean there wasn’t a function that existed to aid in the process if by nothing else, the documentation itself. Let’s take a look at how […]

Benchmarking PHP code

Have you ever asked yourself, “I wonder which of these blocks of code will run faster?” I know I have and I use a very basic template for setting up these benchmarking experiments. The gist of the script is to run through each block of code n times tracking the […]

How to generate the Fibonacci sequence with PHP

The Fibonacci sequence is a sequence of numbers that are derived from the previous numbers in the sequence. The sequence starts at zero and each subsequent number is sum of the previous two numbers in the sequence. There are a number of ways to accomplish this and the most common […]

Stop Accommodating Shortcomings

I came to a very liberating conclusion the other day. I need to stop fucking around with MySQL, or any other RDBMS, and fully embrace a NoSQL server as my primary data store. I’ve been a huge fan of Redis for quite some time but the fact that everything lives […]

How to connect to MongoDB using PHP

I’ve previously discussed installing the MongoDB module but have yet to touch on how to actually do anything past the install. The first thing to do is to connect to the MongoDB server. This can simply be done like this: $client = new MongoClient(); This will connect to the default […]

Thoughts on API design

I recently had the opportunity to have a discussion about API design and RESTful services with Vasusen Patil. I’ve also been having quite a few conversations of similar nature with Justin Davis over the last few months. These types of conversations are some of my favorite because, for the most […]

How to merge two sorted PHP arrays without array_merge() or sort()

I’ve had this question come up a few times in the past on interviews, how would you merge two sorted arrays and keep their sort order intact? The easy / PHP answer is to simply use array_merge() and then sort() the resulting array. Technically this works but the whole point […]

Visibility in PHP

Visibility of functions and properties is very important when you are attempting to lock down certain aspects of an object. In PHP there are three levels of visibility, public, private and protected. Let’s take a look at what each one means. Public Public is the easy one. It’s the default […]

The definition of “done”

For me, “done” means that something I have been working on is to the point that I no longer have to work on it, indefinitely. Depending on the nature of the task it could also mean that it’s been shipped to production and again, no longer requires my efforts. I […]

Simple PHP i18n internationalization and localization class

As sites and system grow and scale, the need for internationalization and localization (i18n and L10n respectively) of content becomes a necessary task. There’s not much to it on the surface, you need to be able to serve up the same content in different languages. This includes abstracting out words […]

Debugging Invalid XML in PHP

SimpleXML with all of it’s faults is still a great way to interact with XML. Most of it’s shortcomings are related to debugging and how it handles invalid XML is no exception. Let’s take a look at what happens when we load an invalid XML string: $xml = " <?xml […]

Writing unit tests for legacy code

I think one of my biggest gripes with adopting that TDD life is that I feel I’m in the minority with my friends even though the concept appears to be quite mainstream these days. One of the common pushbacks I receive is that it’s hard to write unit tests for […]

jQuery plugin to calculate read time

I love hosting my blog on GitHub by way of Jekyll but the Liquid templating system is far from a robust scripting language in my opinion. With the rise of Medium, I’ve been seeing the read time on more and more articles and figured it was time to add the […]

Fizz Buzz in PHP

Fizz buzz is a fairly common screening question to help sniff out non-programmers during the interview process. The task is to print out numbers 1 through 100 but for multiples of 3 print out “Fizz” instead of the number and for multiples of 5 print “Buzz” instead. If the number […]

How to execute an HTTP POST using PHP cURL

I’ve been doing a lot of API integration recently and cURL ends up being exceptionally handy when you are POSTing to an endpoint. Fortunately using cURL in PHP is really easy: // Sets our destination URL $endpoint_url = 'https://somesite.com/path/to/endpoint'; // Creates our data array that we want to post to […]

Looping through a date range with PHP

Looping through a date range isn’t as simple as looping through an array with foreach but it doesn’t take much additional code to get the job done. To loop through a date range you will need to use a loop (I prefer a while loop) and increment the date by […]

Difference between break; and continue; in PHP

The topic of break and continue came up the other day and I thought it would make for a good blog post. You’re probably familiar with break moreso than continue because it is part of a switch statement’s syntax. Both break and continue can also be used inside of looping […]

PHP without fancy braces

PHP is a C-style language and thus uses fancy braces to wrap or hug control structures. PHP is also a language that is extremely flexible so you can also get by without using fancy braces. You will never get away from them entirely as functions and classes / class methods […]

Debugging SimpleXML Objects with PHP

SimpleXML is pretty much the de facto standard for dealing with XML in PHP. The biggest problem with SimpleXML is that it is a pseudo-object that you can’t print_r() or var_dump() to see what’s going on. I recently ran into a situation where it would have been super nice to […]

Parsing SOAP responses with PHP's SimpleXML

We recently discussed parsing XML with SimpleXML and since that writing I ran into a situation where I needed to parse a SOAP response. As it turns out, SOAP responses are a totally different beast when it comes to parsing them with SimpleXML. SOAP responses contain namespaces on some if […]

How to calculate Independence Day observances with PHP

On this 238th anniversary of the United States of America, I felt it appropriate to post an article on how to determine what day the July 4th National Holiday is observed on. There are two rules to the observances. First, if the holiday falls on a Saturday, it is observed […]

The importance of a staging environment

Full disclosure, I don’t actually have a staging server for my personal projects. I develop locally in a very similar environment as my production server and any time I have to do something major, like upgrading the OS or upgrading software between major revisions, I always do a dry run […]

How to parse XML with PHP's SimpleXML

Parsing XML in PHP couldn’t be easier thanks to the SimpleXML extension. SimpleXML allows you to read in an XML string, verify that it is in fact XML and can create an object that you can interact with by way of the node names in the XML.The SimpleXML extension also […]