No Arrow Keys in Vim

" Get off my lawn
nnoremap  :echoe "Use h"
nnoremap  :echoe "Use l"
nnoremap  :echoe "Use k"
nnoremap  :echoe "Use j"

Interesting. Maybe I should put these lines into the dot file to prevent the intern from using the arrow keys?

Reinstall Ubuntu and Update All Packages.

As far as I remember, every major Ubuntu upgrades will fail me in one way or another. This time I accidentally switched off the lappy while upgrading, causing corruption to the filesystem and making it unmountable. Luckily, rescue mode help me to backup all the files before a reinstallation.

Bootable USB live thumb drive.
Since my lappy has no CDROM, I will need to download the ISO and burn it to a thumb drive. Creating a bootable USB thumb drive is done through Unetbootin.

Apt-get Mirror Servers.
It seems that our local Ubuntu update servers are slower compare to our neighbour country, Singapore. On average, I can get 50 - 60 kB/s from .SG server where .MY server only gave me downloading speed of 20 - 30 kB/s. Why ? I have no idea what so ever. Can someone enlighten me on this?

To change the update servers,
$ sudo -s
$ cd /etc/apt
$ sed -i.orig 's/my/sg/' sources.list

Some explanation on the sed command. It basically tell sed
  • to modify the sources.list in place (save in the same file)
  • make backup of the original file with .orig sufffix.
  • substitute my with sg
Further reading on the sed command.

Then we continue with the update and upgrade.
$ apt-get update; apt-get -y upgrade

More steps ahead.

Subversion Keyword Substitution

I don't know when and why this this started but it has become an old habit of mine when I inherit any legacy projects.

Put all $Id$ tag at head of the file after the <?php opening tag. Note that the keyword Id is a substitution for Revision, Date, and Author.
// $Id$

Set keywords property to all php files
$ svn -R propset svn:keywords "Id" *

Commit the property changes and update local copy
$ svn commit -m "set keyword property" *
$ svn up

See the result
$ cat test.php

// $Id: test.php 12 2011-08-28 13:09:48Z foobar $

Search and Replace Strings Without Using find and sed

Using replace (utility that came with mysql)
$ sudo apt-get install mysql
$ replace foo bar *.php

Using rpl
$sudo apt-get install rpl
$ rpl -R foo bar *.php

Declaring CONSTANT in PHP

Instead of checking and declaring constant in this way
if ( ! defined('FOOBAR') ) define('FOOBAR', '123');

We can rewrite it as
defined('FOOBAR') or define('FOOOBAR', '123');

unexpected T_ECHO

Having extra space between <? php gave you all soft of unhelpful error message.

< ? php
echo '123';

$ php -f foobar.php

php -f t.php 
PHP Parse error: syntax error, unexpected T_ECHO in /tmp/foobar.php on line 3

Difference Between require_once And require

Really caught me by surprise this morning. The poor intern had to manually click the item one by one.

foreach ( range(0, 4) as $no )
require_once 'a.php';
require 'b.php';

<?php echo "a:$no\n"; ?>

<?php echo "a:$no\n"; ?>

$ php -f foobar.php
a:0 <----- notice that a.php only include ONCE

Split a String Into Array Without Delimiter

Following advice from StackOverflow.
$ php -a

php > print_r(str_split('foobar', 1));
[0] => f
[1] => o
[2] => o
[3] => b
[4] => a
[5] => r

Tidy Up XML File

Some googling around, between tidy and xml_pp, the later format a better looking XML file.
$ sudo apt-get install xml-twig-tools
$ xml_pp foo.xml > bar.xml

PHP's DateTime between 5.2 and 5.3

Is very annoying to have different PHP version between development, staging, live and client machine. Regardless what, must force everyone to upgrade to version 5.3.x.

$datetime = new DateTime();
$datetime->add(new DateInterval('P1D')); // 5.3
$datetime->modify('+1 day'); // 5.2

$datetime = new DateTime();
$datetime->getTimestamp(); // 5.3
$datetime->format('U'); // 5.2