PHP Articles

How to disable the X-Mailer header with PHPMailer

Sometimes things aren’t quite as you’d expect them to be. One such case is when you try to disable the X-Mailer header with the PHPMailer library. The library itself is pretty straight forward, you instantiate the class, and then you can interact with headers as object properties as such: $mailer […]

Filtering WordPress menu items

Recently I had a request come in to update the navigation on a WordPress site. The way the request was worded made me think the change to the menu needed to happen on one specific page, and not across the entire site. This created an interesting problem, as the theme […]

Time zone conversion with PHP

I do not like time zones. They’ve been a fairly regular pain point in my career, especially early on. There was even a time when I built something and totally forgot that time zones existed and well, it was a mess. For the most part, time zones don’t cause me […]

Setting an authorization header when using file_get_contents with PHP

Going through some of my old lists of blog post ideas this weekend. While it’s not something I’ve needed for a minute, it’s still something worth taking about. How to set an authorization header when using PHP’s wonderful file_get_contents() method. Similar to an old post of mine talking about specifying […]

Juggling assertion syntax between PHPUnit versions

I’m no stranger to the nuances of testing against different version of PHP and PHPUnit, but my previous issues were related to the environment itself. Recently I ran into an issue with different syntax between versions of PHPUnit, specifically assertRegExp() being deprecated, which was throwing an error. Not only was […]

Passing a test without assertions with PHPUnit

While it may seem counter intuitive to have a test that doesn’t have any assertions, PHPUnit’s unique method of sharing data between tests can put you in a situation where you have a test that just doesn’t need to actually test anything, and simply returns some data. In those scenarios, […]

Increase memory limit for Composer

Gone are the days of stitching together disparate services in an effort to create a solid continue integration and deployment pipeline. GitLab has had it’s Runners for a while and recently GitHub stepped into the mix with their Actions offering. Because of this, using external services like Travis CI or […]

Only run step for specific version with GitHub Actions

With my recent exploration of GitHub Actions, I’ve had to figure out things that I’ve already figured out with other systems like GitLab Runners and Travis CI. One such thing is conditionally running steps. For things like code coverage, I’m only interested in sending off the coverage report to the […]

How to loop through all files in an S3 bucket in PHP

Recently I decided to finally tackle the ever growing S3 bucket for the niche social network I run. The reason it’s ever growing is because I never implemented any sort of hard deletion logic. At one point, I was planning to move the images over to soft deletions, but never […]

Symlinks with Nginx and PHP-FPM

Recently I’ve been putting time into rebuilding the server infrastructure for Holiday API. This has included hardening the system to be more fault tolerant and building out a new cluster of web servers. The biggest improvement was [finally] introducing a continuous deployment pipeline to complement the existing continuous integration flow. […]

How to calculate United States holiday observances

Usually around a holiday I will do a post that’s somehow related to the holiday. Seemed kind of silly to do a post on how to calculate Independence Day in the United States since it’s pretty straight forward. It always lands on the 4th day of the month of July. […]

How to POST to a webhook in 5 different programming languages

With the release of inbound webhooks for CrowdSync workflows last week, it seemed fitting to do a write up on how to POST to a webhook. So what is a webhook, anyway? The Wikipedia definition is that webhooks are “used-defined HTTP callbacks”. Kind of a stuffy definition if you ask […]

URL routing in PHP

The other day I was checking out what folks are querying to get to my site. Of course a ton of people get here looking for VPS comparison, but what surprised me was the volume of queries for “PHP routing”. Back in 2015 I had posted a basic page rounding […]

PHPUnit with multiple versions of PHP and HHVM on Travis CI

Recently I was updating my PHP Lorem Ipsum library in an attempt to get it testing on HHVM on Travis CI alongside the other versions of PHP I was testing against. Incidentally, the last time I had worked on the project, I had to work out some kinks with older […]

Upgrade to PHP 7.x on Ubuntu 14.04 LTS

Upgrading to PHP 7.x (7.0 or 7.1 as of the time of this writing) is just as easy as it is to upgrade to PHP 5.6! Before we install PHP 7.x, I would recommend getting your system up to date: sudo apt-get update Next you will need to add the […]

I beat PHP and so can you!

By request from Bill Martin, an abridged history of my relationship with the bastard child of programming languages, PHP. Ahh PHP. I love it. I hate it. For well over a decade, I slung PHP day and night. I got into PHP at the tail end of version 3. Prior […]

Reading a specific line from a file with PHP

As I’ve been building a system that is driven primarily by CSV files (because it’s easier to use Google Sheets than it is to build out an admin section) I’ve had to get creative. Recently I found myself working with a CSV file that’s around of 3000 lines of data […]

Convert Mongo BSONDocument to an array in PHP

Ran into a scenario this weekend where I needed to take a sub-document from Mongo but I needed it in an array in PHP. Reading the data from Mongo with PHP was simple and won’t be discussed. What I’d like to talk about is taking the BSONDocument that Mongo returns […]

Programming the same thing in multiple programming languages

Recently I set out on an interesting journey. I wanted to build programming libraries for my API, HolidayAPI. Some of the languages I know like the back of my hand. Others, have never even touched them before. Before you start sending me hate comment, I am fully aware of Swagger. […]

Get the day of the week from a date in PHP

Typical of PHP, it’s very easy to get the day of the week from a date, but they also provide the data in multiple format. Three in fact. The first, and arguably the most straight forward is to get the textual name of the day of the week. <?php echo […]

Calculate time left in day in PHP

Before PHP 5.3, finding the difference between two dates was a bit of a pain. Convert the date strings to UNIX timestamps (if they weren’t already), subtract them, and then divide the result until you had each of the components (seconds, minutes, et cetera). As of PHP 5.3+ you can […]

Email validation via MX lookup in PHP

Validating that an email addresses ain’t what it used to be. I remember writing simple regular expressions that worked out really well. These days there are a bajillion TLDs with more being added all of the time. Something I started doing is checking that the domain of the email being […]

Check if a string contains a character with PHP

Checking if a string contains a character could be accomplished easily with a regular expression or you can use the built-in PHP functions. To check if a string contains a character of a specific case you can use strpos. $haystack = 'This is my haystack that we shall check' $has_A […]

Generate random hex color with PHP

There’s many ways to skin a cat, or generate a color. You could randomize a number between 0 and 16777215 then convert it to hex: $rand_color = '#' . dechex(mt_rant(0, 16777215)); Or you could do what I like to do, just md5 a random string and grab the first 6 […]

Run a block of PHP code a certain percentage of the time

Sometimes, you may want to run code all of the time. Other times, you may want to run code some of the time. This is how garbage collection in PHP works. Based on your configuration for gc_probability and gc_divisor, garbage collections runs a fraction of the time (defaulting to 1/100 […]