Page 5 of PHP Articles

How to write files to disk with PHP

Saving files to disk is a pretty simple task in PHP with file_put_contents(). Here’s how to write a string to a file: $string = 'I AM STRING!!'; file_put_contents('/path/to/destination', $string); It is generally good practice to only do with when you are dealing with smaller strings as you can run into […]

How to use try/catch statements in PHP. Oh, and finally too!

I do a lot of coding in Python and one thing that I really love is the mindset of asking for forgiveness instead of permission. What’s this mean? It means that instead of sanity checking every little thing, just handling the exceptions that are thrown instead. In PHP this can […]

Your stack is outdated

I’ve been noticing a pattern with most of my peers (yes I said most), they expect their users to be running the latest and greatest version of their favorite “modern” browser and all the while their server stack is collecting dust. Ubuntu doesn’t help matters with their Long Term Service […]

How to use switch/case statements in PHP

switch/case statements are one of my favorite anti-patterns not because I prefer to write spaghetti code but because they can make if/elseif blocks look a ton cleaner. Why would it be considered an anti-pattern? Because it could easily be abused to the point that you are munging up your control […]

Improve performance by reusing PHP objects

I’ve been doing a ton of optimizations on one of my main sites and one thing that I’ve been doing more and more is trying to reuse objects whenever possible. What’s this entail? All it takes is creating a static function to return an instance of the object instead of […]

How I saw the test-driven light

Aside from dealing with financial transactions, I’ve generally avoided test-driven development as part of my day to day workflow. Why? The usual reasons, but mostly because I didn’t want to incur the overhead of additional development. As a single founder / developer you’re always looking for ways to lighten the […]

PHP 5.5 syntax highlighting for Vim

Now that I’m using PHP 5.5 I’m finding that my Vim syntax highlighting is a bit off. After some digging around I found a syntax file that was updated to PHP 5.5 RC1 but hasn’t been updated since. In true hacker fashion I went ahead and forked the project and […]

Upgrade from PHP 5.3 to PHP 5.5 on Ubuntu 12.04 LTS

Recently I made the decision to stop living in the past and start living in the present by using the latest stable release of PHP, version 5.5.8. Unfortunately on Ubuntu 12.04 LTS (I always run LTS releases on my servers) the latest version of PHP available is from the 5.3 […]

PHP segmentation faulting from logging too much

At least I think that’s what was happening. Today I made the somewhat calculated risk of upgrading my production server from PHP 5.3.10 to PHP 5.5.7. Scary stuff, jumping 2 point revisions like that but I was feeling cocky after jumping 3 point revisions to bring nginx up to the […]

Getting PHP's version from the command-line

As you may already know, obtaining the version number of PHP is a simple php --version away. This is great if you just want to see the version number, but what if you wanted just the version number? First option would be to use write out some shell script to […]

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 […]

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 […]