Tag: PHP
-
Incrementing / Decrementing a Number by a Number with PHP
We’ve previously discussed how to simply increment and decrement a variable but that only covered incrementing by a value of 1. What about when you want to increment a variable by 5s? There’s a few different ways this can be accomplished. First, the obvious (albeit far from ideal) using ++…
-
Sorting an associative array by a specific key with PHP
One of PHP’s most powerful data types is the array. It can hold numbers, it can hold letters, it can be sorted, sliced and chunked. Speaking of sorting, when sorting an array the straight forward sorting functions are only for sorting by the keys or by the values. What about…
-
How to convert a CSV file into an array with PHP
Loading CSV files into an array is something that comes up more times than I’d like to admit. Scrubbing mailing lists, loading flat files to a database and various other use cases are out there and fortunately, PHP makes it easy to to do. There are a few ways to…
-
How to Determine if it is Friday the 13th with PHP
It seemed fitting that my post on Friday the 13th would be about Friday the 13th. There are 52 Fridays in 2013, but only 2 of them are the 13th day of the month (September and December). To determine if a Friday is a Friday the 13th, we will need…
-
Building a Number Guessing Game in PHP
Believe it or not, but the last 13 or so posts have all been leading up to this one. Generating random numbers, capturing user input, using colors on the command-line, pluralizing words, and then some are all being utilized in this simple number guessing game. The script itself generates a…
-
How to Terminate a PHP Script
Terminating the execution of a script is usually one of those last ditch ways to handle an error, but it is something we do from time to time, especially if we’re debugging. To terminate the execution of a script, you can call either exit or die, die being an alias…
-
How to Pluralize Words with PHP
There’s a good chance you’ve seen it before (and possibly authored it), it looks something like this “You have 1 new messages” or “There are 10 user(s) logged in”. What you end up with is something that’s either incorrect or something like looks a bit amateurish. To fix this, we…
-
How to Output Variables within Strings with PHP
Displaying strings isn’t that hard, you just use echo followed by the string in question. But that only gets us so far, what about when we need to use variables inside of the string? Luckily there’s a ton of different ways to accomplish this. echo You can easily use echo…
-
How to Use Colors in Command-line PHP Output
Nothing spruces up command-line output the way colors do. Just like when you’re customizing your [Bash] prompt, you can use color codes in strings to colorize your output. Keep in mind, these colors will only work on the command-line and not in a browser. First let’s take a look at…
-
How to Compare Numeric Variables with PHP
Comparing numeric values is a very common programming task. Which number is greater? is lesser? equal? In PHP like any C-style language, you can do these comparisons with a simple if ( … ) { … } statement: if ($var1 > $var2) { echo $var1 . ‘ is greater than…