Short variable swap in PHP > 5.4.x

Following this great old post from David Walsh’s post Tweet for Code #2, here is a PHP adaptation for this JavaScript Var Swap tweet:

$b = [$a, $a = $b][0];

Works on PHP 5.4 and up.

I know this is not very practical, for daily work, but it can come handy in a job interview.

♦ ♦ ♦

[Update:June 16, 2016]

In PHP 7.1.x it will finally be possible to use a cleaner swap short-code:

[$a, $b] = [$b, $a];


Installing ODBTP on Linux with PHP 5.4

Lately I encountered several opportunities to try and install the ODBTP library, at first I was working on new systems, so when i encountered errors when simply doing pecl install odbtp I tried installing from source, encountering errors during the make command I came to my senses and managed just fine using the basic MSSQL library in PHP.
Then a few weeks ago I needed to upgrade an existing system relying on ODBTP with tight coupling, so i had no choice but to find a way to overcome the errors.
After research on the Internet I came to solution but had no time to document the final solution, until today when I installed a test zone on a separate system and remembered only vaguely what I was looking for.
Follow the instructions here except for this: After moving the php/ext content, patch the php_odbtp.c file using the attached file.
This patch was checked on Gentoo linux, Ubuntu server 12.04.1 and Linux mint 14, the variable as far that I could gather from the complaints is the PHP version.

Apache and mime types

A few days ago I started a project of upgrading a CRM system written in PHP.

Now, the current server is using Apache 1.3.33 and PHP 5.1.6, old, right?, and, hold on, the server has a dual core x86 CPU, 4Gb of memory, which holds it for day to day use, but starts to squeak every now and then.

The new server has 8 cores, 8Gb of memory which can be upgraded since it is a Virtual Machine, a 64bit version of gentoo linux installed, and it runs Apache 2.2.24 with PHP 5.4.13 as a mod and is constantly updated.

I thought that the PHP syntax and function changes would be a pain, apparently that was nothing, Apache upgrade and non-planned configurations in the php code and file namings, they made the transfer more difficult.

The problem was that one specific file, a JS dictionary file with the name dict.hebrew.utf-8.js was being served with the encoding iso-8859-8 (Visual Hebrew), what made all the content seem like gibberish, funny, hu?

Snooping and searching the web, I tried simply setting the AddDefaultCharset UTF-8, but that did nothing, so I looked at my wall and saw the page saying RTFM, and so I did…

Then it hit me, the setting AddCharset iso-8859-8 .hebrew is set before AddCharset UTF-8 .utf8 .utf-8 (I added the last one for compatibility) so I commented out the utf8 line and copied it to be the first AddCharset line, and WALLA, it did it.

So, to conclude my messy post… Remember Remember

  • First come first served
  • Name static files with language or encoding names thoughtfully
  • Try and read what the plans are for the technologies you are using for the system you are building
  • If your system has some hiccups, try to RTFM
  • The fifth of November


Drupal form ajax & Zurb Foundation3

A few months ago I embedded the Foundation3 Framework into Drupal as the base of a theme for an intra-net system.

At first I found some issues with loading both jquery.js and foundation.js, so I loaded the minimized foundation.js instead of jquery.js and all worked fine.

Last week i added a form to one of the modules that uses AJAX refresh, and here I bumped into a tough one, I received an error stating that handleError is not defined (actualy, it was: function [something] has no method handleError).

So as usual, I snooped around the Internet, and of course, jQuery deprecated handleError, so some suggested to add the function in the page before the triggering function is called, it only helped me see the error, not prevent it from being raised.

So I realized, that the reason the handleError is even called is because it depends on an old jQuery version, and with foundation I'm using a newer one.

In short, I added the latest jquery.form.js from malsup's website to the theme, and in template.php replaced misc/jquery.form.js with the new one, like this:

function THEMENAME_js_alter(&$js) {
  if (isset($js['misc/jquery.form.js'])) {
    $path = drupal_get_path('theme', 'THEMENAME') . '/js/jquery.form.js';
    $js[$path] = $js['misc/jquery.form.js'];
    $js[$path]['version'] = '3.27';
    $js[$path]['data'] = $path;