How to have fun with your Linux server

Posted on Wednesday, June 17th, 2009 under , ,

I’ve found a great way to have fun with your linux server. First of all, install cowsay. If you use Ubuntu server or Debian, you can use apt-get install it:

sudo apt-get install cowsay

Now, you have to wait for another user to connect to the server via ssh. You can use the who command to see who is logged in. With two open terminals on my Ubuntu box, who’s output looks something like this.

tudor@thor:~$ who
tudor    tty7         2009-06-17 20:31 (:0)
tudor    pts/0        2009-06-17 20:36 (:0.0)
tudor    pts/2        2009-06-17 22:21 (:0.0)

Let’s assume that the victim is the one using the first terminal, pts/0. Now, all you have to do is punch the following like in your terminal:

cowsay -f tux "I see you" > /dev/pts/0

…and his terminal will look like this:

tux

Doesn’t this make you feel better about yourself?

Later edit: the party continues here!

The Vendor Client relationship – in real world situations

Posted on Tuesday, June 16th, 2009 under ,

An incredibly cool movie.

It’s funny because is true

Coding music

Posted on Tuesday, April 14th, 2009 under ,

What do you do while programming? And more important, what kind of music do you listen while programming? My habits are:

  • Starting a new project: Rammstein – Sonne
  • Taking over a project originally started by a junior programmer: Nightwish – Wish I had an angel
  • I’m assigned to 5 years old project that uses really old technologies and frameworks: Weird Al Yankovich – Amish paradise
  • Looking over a Perl script written by somebody else: Prodigy – Voodoo people
  • After a long meeting with the project manager: Zebda – Hasta siempre
  • Debugging (first 30 minutes): Rammstein – Ich will
  • Debugging (after 30 minutes): Prodigy – Fire starter
  • Looking for security flaws: MC Hammer – Can’t touch this
  • When I see a QA guy heading for my desk: Nana – He’s coming
  • When the project goes online: Britney Spears – Ops, I did it again (just kidding :P ) I’m having a brew and listen to Orthodox Celts – Star of the county down

What do you listen to while coding?

The six stages of debugging

Posted on Wednesday, February 25th, 2009 under ,
  1. That can’t happen
  2. That doesn’t happen on my machine
  3. That shouldn’t happen
  4. Why does that happen?
  5. Oh, I see
  6. How did that ever work?

via Mihai Brehar…and if debugging means taking the bugs out, then programming means putting the bugs in :P

Dynamic or variable variables in PHP

Posted on Tuesday, February 3rd, 2009 under , , ,

Variable variables (or their official name – pointed out by Ionut Stan in the comments) or “dynamic variables” (how I like to call them) is the name of a feature of the PHP programming language which allows a programmer to define a variable that has its name given by the content of another variable. Like such:

$foo = 'bar';
/*
 * declare a variable called bar into the 
 * current scope and initialise it with Hello world
 */
$$foo = 'Hello world';
 
echo $bar; // will echo Hello world

If find this really useless and very annoying, because a typo such as a double dollar sign in front of a variable can result in some very hard to track bugs. But this isn’t just another strange feature that made its way into the trunk. This is very, very well implemented and some programmers strived a lot to make it work under all possible circumstances. It works so well that you can use strings that cannot be normally used to represent variable names, such as !@#$%^&*()_+-=/*. Really, it works!

$foo = '!@#$%^&*()_+-=/*';
$$foo = 'Hello world';
 
$variables = get_defined_vars();
echo $variables['!@#$%^&*()_+-=/*']; 
/**
 * will echo Hello world - the content of a variable called !@#$%^&*()_+-=/*
 */

Further more, it even works with objects:

class Example {
     /**
      * sample attribute
      * 
      * @var string
      */
     private $value;
 
     /**
      * default constructor
      * 
      * @param $_value
      */
     public function __construct( $_value ) {
          $this->value = (string) $_value;
     }
 
     /**
      * returns a string representation
      * of this object
      * 
      * @return string
      */
     public function __toString() {
          return $this->value;
     }
}
 
$foo = new Example( 'bar' );
$$foo = 'Hello world';
 
echo $bar; // will echo Hello world

I think this one of those features reserved for the most gurus of the gurus, because after 5 years experience with PHP and a Zend Certification, I haven’t yet grasped the logic behind this…

Common lies in freelancing

Posted on Thursday, January 29th, 2009 under , ,

At one time in our careers, we all worked as freelancers. It’s fun, you get to pick your work, no annoying project managers, no accounts managers, no managers at all. You’re your own boss. Just you and the project. And yeah, almost forgot, the client.

Because most freelancers are cowboy programmers that work from home, sooner or later the project will fall behind schedule. Reasons include proximity to the fridge (also has a devastating effect on the waist), games that are installed on that computer, movies and so on. What do you think most people will choose between eating a huge sandwich and drinking some beer while watching the new episode of Heroes and working? Yeah, my point exactly.

And when you’re behind schedule and the client asks you for a status update, you’ll do what every other moral person would. Lie about it! But try to do it with style and in a way that won’t get you caught. Most common lies are:

  • I’ve worked on the database” since nobody can actually check that
  • I’ve solved some security issues” again, really hard for somebody to actually check that
  • We have a small problem with the server’s configuration, I’m working on it now” somehow implies that is not your fault
  • I haven’t done the layout part yet, but everything else is ready” but I can’t show it to you without the layout now, can I?
  • My (retarded) neighbour flooded me, but don’t worry, your files are safe” should work, as long as he’s not your neighbour…
  • We’re almost there, I’ll set up a demo environment for you by tomorrow noon” like coping some files on a FTP host takes a day and a half
  • I’ve worked on optimization…riiiiiiiiiiiiight

And in the end, a meaningful quote: I did not have sexual relations with that woman – Bill Clinton.

About the ++ incrementer

Posted on Tuesday, January 27th, 2009 under , ,

Few weeks ago I was in the middle of a debate with my girlfriend – which is also a programmer – about the increment unary operator. The main topic was what will the following code display:

$i = 1;
$i = $i++;
echo $i;

I said 1, she said 2. Of course, I was right in the end. But we were having this conversation while we were heading out to meet some friends, so, inevitably, others were drawn into the debate. One of which is Costin, who posted on his blog an entry called The trials and tribulations of the C incrementer on this subject. Long story short, if you post increment a variable, this operation will take place last and its result will be poped first from the stack and in some cases will be lost (our case – read Costin’s post for more details).

Other annoying examples using the increment operator:

$i = 1;
function someFunction( $_value ) {
    echo $_value;
}
 
someFunction( $i++ ); // will display 1
someFunction( ++$i ); // will display 2
 
$j = 1;
$j = ++$j;
echo $j; // will display 2

And again, the ZCE exam features this kind of questions.

Variable interchange

Posted on Monday, January 19th, 2009 under , , ,

How to interchange 2 variables? A simple question with a simple answer. Most people will do it like this:

// interchanging using a temporary variable
$a = 2;
$b = 3;
 
$temp = $a;
$a = $b;
$b = $temp;
 
echo $a . ' ' . $b . PHP_EOL; // will display 3 2

Quite elementary. But it can be done in a much more elegant manner, using arithmetical (addition & subtraction) or logical (xor) operators. Keep reading»

Language constructs fun

Posted on Friday, January 16th, 2009 under , ,

What will the following code produce?

echo '1' . print( 2 ) + 3;

Tough question? Not quite. Just run the code and find out the answer. If you’re too lazy or don’t have a PHP server at hand, I’ll tell you the result. It will print 511 to the screen. Now comes the tough question. Why is that? We – me and my colleagues at Zitec – sat quite a while before figuring it out. It goes like this (special thanks go the Alex “the badger” Novac – see his pic on Zitec’s Team page): print is a language construct. It doesn’t need brackets to work. The following lines do the same thing:

print( 5 ); // will print 5
print 5; // again, will print 5

Thus, the line print(2) + 3 will translate as print 5, because the brackets are used “arithmetically” here, stating that their content should be evaluated first. Basically it happens like this: it evaluates (2) to 2, adds 3 and prints the result. This will give us the 5 on the first position. As stated in the php manual, print always returns 1. So after displaying the first 5, the code becomes echo '1' . 1. Pretty self explanatory.

If you want to take the Zend Certified Engineer exam, expect this type of questions.