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 caveat is to store every single number in the sequence instead of just the...

Read Full Article →

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 server (localhost) and port (27017). If you want to connect to a different server...

Read Full Article →

How to merge two sorted 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 behind the question is to see if you can work through the logic itself....

Read Full Article →

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 visibility and it means any and everyone has access to it. If you come...

Read Full Article →

Simple 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 and phrases of content into variables that can then be populated with the the...

Read Full Article →