Page 4 of PHP Articles

Parsing SOAP responses with PHP's SimpleXML

We recently discussed parsing XML with SimpleXML and since that writing I ran into a situation where I needed to parse a SOAP response. As it turns out, SOAP responses are a totally different beast when it comes to parsing them with SimpleXML. SOAP responses contain namespaces on some if […]

How to calculate Independence Day observances with PHP

On this 238th anniversary of the United States of America, I felt it appropriate to post an article on how to determine what day the July 4th National Holiday is observed on. There are two rules to the observances. First, if the holiday falls on a Saturday, it is observed […]

How to parse XML with PHP's SimpleXML

Parsing XML in PHP couldn’t be easier thanks to the SimpleXML extension. SimpleXML allows you to read in an XML string, verify that it is in fact XML and can create an object that you can interact with by way of the node names in the XML.The SimpleXML extension also […]

Checking your IMAP email using PHP

I have an upcoming freelance project coming up that includes checking an IMAP email account as well as working with XML in PHP. Let’s talk about connecting to an IMAP account in PHP, don’t worry, I’ll discuss parsing XML next week ;) For the sake of example, we’re going to […]

How to calculate Mother's Day and Father's Day with PHP

A little late on the Mother’s Day part, but since today is Father’s Day, I figured it would be fun to discuss how to obtain the dates for these moving holidays. To do so, all we need to use is use strtotime and tell it when the holiday will occur […]

How to pipeline with phpredis

Have you ever run into a scenario where you have to loop through a set of data and perform database queries for each iteration? Obviously we shouldn’t run them one by one, so we end up using a TRANSACTION or even just building out one large query in the case […]

How to process PUT requests with PHP

You are probably already familiar with $_GET, $_POST and $_REQUEST but what about $_PUT? Trick question, $_PUT (as well as as $_HEAD and $_DELETE) doesn’t exist in PHP at this time. Never fear! We can create a $_PUT array with the following code: if ($_SERVER['REQUEST_METHOD'] == 'PUT') { parse_str(file_get_contents("php://input"), $_PUT); […]

URL Routing with PHP

I’ve been taking a lot of programming tests / challenges recently as I was in the market for a new [and hopefully exciting] opportunity to spend my time on. Because of this, I am full of new blog post ideas based on the questions / challenges asked. This week I’m […]

Google Places PHP Library

I tend to shy away from these self gratifying posts about my own open source contributions, but I’m going to make the exception because this particularly library has been receiving quote a few downloads this month. I’m unsure for the increase, but my Google Places PHP Library has had over […]

How to Install PHP Redis on Ubuntu 14.04 LTS

Previously, I had shown you how to install PHP Redis from source but things changed with the latest Ubuntu LTS release. Now you can install the phpredis extension from the Ubuntu respositories. First, if you don’t have it installed already, let’s install Redis: sudo apt-get install redis-server After we get […]

How to Install the MongoDB PHP Module on Ubuntu 14.04 LTS

Installing the PHP module for MongoDB on Ubuntu 14.04 LTS is just a command away, but first, let’s ensure that we have MongoDB itself installed: sudo apt-get install mongodb Once we have MongoDB installed, we can proceed with installing the module. Please note that you could combine these commands to […]

How to Setup a LEMP Stack on Ubuntu 14.04 LTS

Last week I covered setting up a LAMP server on Ubuntu 14.04 LTS but for my money, it’s all about LEMP stacks. The “E” actually stands for Nginx (pronouced Engine X) and it’s an altnerative to the Apache web server that is built for speed and has a very low […]

How to Setup a LAMP Stack on Ubuntu 14.04 LTS

The newest long term support release of Ubuntu (Trusty Tahr) is finally here! I just spun up a droplet over on DigitalOcean to walk through setting up a LAMP stack for this post. I also recommend making sure that your system is completely up to date. At the time of […]

Compressing and uncompressing a string with PHP

Today I went on a mission to find a PHP function that I’ve never used before and knew nothing about. The mission was such a success that I have two functions to discuss and honestly, I’m pretty sure I’ll never use them. That being said, if the need ever arises […]

PHP's /e modifier is deprecated, use preg_replace_callback instead

I’ve discussed the use of preg_replace_callback in the past in regard to passing variables to anonymous functions but never touched on how to use preg_replace_callback or the fact that the /e modifier has been deprecated in PHP 5.5+. Even though /e still works in PHP 5.5 in the near future […]

Transactions with PHP Data Objects (PDO)

PHP Data Objects (PDO) is arguably the best database abstraction layer available in PHP. It provides a consistent interface across multiple datasources (MySQL, PostgreSQL, et cetera) and helps boost your site’s security by way of prepared statements. It even supports transactions which we’ll be taking a look at here. Database […]

How to zero fill a number with PHP

Zero filling (or zero padding) is when you pad a value on the left side to a specific length. This is something you will commonly see in database schemas for integer columns. But what if we’re not working with data that’s already zero filled by a database? All we have […]

How to get keys from an associative array in PHP

One of PHP’s most powerful features are arrays and the multitude of functions available to interact with and manipulate them. Arrays can be indexed by integers as well as string, known as an associative array. Instead of looping through the array with foreach and assigning the key to another array, […]

Passing variables to anonymous PHP functions

The other day I ran into a situation (first time, it seems) where I needed to access a variable inside of an anonymous function inside of preg_replace_callback(). My first thought, even though it went against my better judgement was to simply define the variables as global inside of the function. […]

Using list() with foreach() in PHP

Continuing my showcasing of all of the awesomeness in PHP 5.5 that I am discovering since my upgrade from 5.3, let’s discuss using the the list() function inside of a foreach() block. Have you ever had a situation where you are looping through a multi-dimensional array and the array is […]

Upgrade from PHP 5.3 to PHP 5.4 on Ubuntu 10.04 LTS

I’ve previously posted about upgrading to PHP 5.5.x on Ubuntu 12.04 LTS but what about my friends still on the even older Ubuntu Long Term Service release that’s still supported, 10.04 LTS. That particular version’s server release will remain supported until April 2015, with more than a year left at […]

How to use Generators in PHP

Support for generators (via the yield keyword) has been added to PHP 5.5 and allows you to create simple iterators without the use of the Iterator interface. What’s this all mean? It boils down to overhead, generators use less memory than an implementation of the Iterator interface or simply creating […]

Warning: Network TCP port is being used by /usr/sbin/php5-fpm. Possible rootkit

Imagine my surprise to see this warning during my morning review of the rootkit checkers that run nightly on my boxes. The thing is, there were no other anomalies on the box aside from /usr/sbin/php5-fpm being bound to a port that was suspected of belonging to a rootkit. The fact […]

String dereferencing or How to extract a single character from a string with PHP

String dereferencing allows you direct access to individual characters. The dereferencing syntax is just like accessing an array element by it’s index: $string = 'This is my awesome string!'; echo 'First Character: ' . $string[0] . "\n"; echo 'Tenth Character: ' . $string[9] . "\n"; echo 'Last Character: ' . […]

Catching multiple PHP exceptions

I had previously discussed using try / catch / finally and a fine reader pointed out that I didn’t mention catching multiple exceptions. He’s right, I dropped the ball and decided to dedicate today’s post to catching multiple exceptions. To catch multiple exceptions you will need to be using code […]