Tag: PHP
-
How to error when setting undefined class properties in PHP
Perhaps you have a class and you absolutely don’t want anyone tacking new properties onto it once it’s been instantiated. To do so, you can use PHP’s overloading to catch the setting of a variable and raise an error accordingly. Here is the default behavior where an object can have…
-
How to set up a LAMP server on Ubuntu 13.10
Been a while since I’ve covered setting up a brand new LAMP server. This time, I’m going to be using Ubuntu 13.10 and once you are logged into your server you will want to update and upgrade it to the latest software versions: sudo apt-get update && apt-get upgrade Once…
-
How to calculate Thanksgiving’s date with PHP
The topic of this post ended up turning into quite the programming exercise for me. In true PHP fashion, I was able to come up with 3 distinct methods to determine what date Thanksgiving falls on (and have another in mind but it’s just a crappier version of what I’m…
-
How to convert a negative number to a positive number with PHP
Converting a number from negative to positive is a pretty simple process using the absolute function abs(): $positive = abs(-123); // = 123 $positive = abs(123); // also = 123 Absolute returns a positive integer or float based on what you feed it. Because it only ever returns a positive…
-
PHP’s HereDoc versus NowDoc
We’ve previously discussed how to assign variables and add in variables and all of that good stuff, but what about if you are dealing with a large block of text? There’s many approaches, most that require you to be mindful of escaping characters like simply using quotes to wrap a…
-
How to handle daylight savings time in PHP
If you’ve ever dealt with time in PHP you’ve probably been burnt by daylight savings time before (quite possibly yesterday ;). I’ve been there, waking up on the Sunday of the time change with an email about timestamps being an hour off or some other anomaly that ends up being…
-
Split an array into chunks with PHP
Array chunking is a great way to paginate large arrays and as I found out recently, makes it very easy to map a large array from Redis (by way of mget and/or pipelining) back to the original data. Chunking an array is the process of taking an array and splitting…
-
Shuffle an associative array with PHP
PHP makes it really easy to randomize the order of an array with the shuffle() function. If you’ve ever used this function on an associative array you know that the array will be randomized, but the keys will be dropped. It takes a bit more work, but you can randomize…
-
Incrementing / Decrementing a String with PHP
In the past, we’ve discussed incrementing and decrementing a variable and incrementing and decrementing a number by another number but did you know that we can also increment and decrement a string? You can use the shorthand operators ++ or — to accomplish this. Please note that you can’t increment…
-
Simple Probability Distribution with PHP
We’ve all been there, trying to juggle two or more things for either split testing or just because you want some variety. This came up recently for me with juggling advertising networks and I wrote some simply code to handle it. The premise is simple, you assign weights to the…