Tag: PHP
-
Days until Christmas in PHP
The holidays are upon us so it’s time for my obligatory holidays themed post! Keep in mind that the title may say Christmas but you could apply this same logic to any holiday or date of your choosing. $christmas = date(‘Y-12-25’); $today = date(‘Y-m-d’); echo (strtotime($christmas) – strtotime($today)) / 86400;…
-
How to reverse a string with PHP
Reversing a string in most languages is a pretty trivial task. In PHP we have a dedicated strrev function to do so: $string = ‘This is my awesome string!’; $reverse = strrev($string); But what if this function didn’t exist? How would you go about reversing the string? For me, I’d…
-
How to generate a date range with PHP
From time to time I need to loop through a date range. One approach is to generate a start date and an end date and then add 1 day to the start date until it reaches the end date (or whatever interval you want to increment by). This works wonders…
-
How to convert a string to an array with PHP
Converting a string to an array is a pretty common task when programming in PHP. If you don’t know much about PHP you could easily fall into a situation where you’re manually looping through each character in a string checking for a delimiter or keeping a counter and assembling strings…
-
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…
-
Why I still use and maintain my own PHP framework
I consider myself a PHP developer first and foremost. I don’t tout myself as a Pickles developer (my own framework) or a WordPress, Laravel, CakePHP, Django, Flask or Meteor developer even though I have working experience with all of them. At the end of the day those frameworks still require…
-
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…