Tag: PHP
-
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…
-
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: ‘ ….
-
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…
-
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…