PHP $_SERVER variables – CLI via SSH vs cron

I have a number of batch processes that run using PHP as the command interpreter, and discovered after making a change to one of them the other day that the variables available to PHP in the $_SERVER superglobal are different depending on whether it’s run via an SSH session or cron. This post shows the difference on Debian 6. The values available are probably the same on other distros.

Categories PHP

Fetch message parts into a flat array with PHP IMAP

The PHP IMAP functions imap_fetchstructure and imap_fetchbody are used to work out the structure of an email and get the message body and attachments, but they can be fiddly to use because the message parts can be nested. This post has a function which effectively flattens the message parts into a new array, indexed by the part number which can be directly passed to imap_fetchbody.

Categories PHP

How to use static variables in a PHP function

There may be times when a static variable is needed in a PHP function; static variables maintain their value between function calls and are tidier than using a global variable because they cannot be modified outside of the function.

Categories PHP

Escape characters in the PHP date function

It’s funny when you’ve been programming in a language for a long time and fall into odd assumptions about particular functions. For some reason I appear to have assumed there was no way to escape characters in PHP’s date function. Then yesterday I saw some sample code where a character was escaped and double checked in the manual. Sure enough, you can escape characters and it’s obvious. I just thought I’d share it here in case you’re making the same mistake I was.

Categories PHP