Category Archives: PHP

Good Old sscanf

Working in PHP, all this time, and the big uses you find in regex, and all the string processing we need to, could make us forget our old friend that can easily solve a problem.
sscanf, do you remember it, well, for quite some time I didn’t, until I saw it in a random code I found online, so this post is a reminder that this function exists.
An example, if we have a date, lets say coming from a Mysql DB, (ofcourse you can use strtotime, but sometimes it has it’s limitations) and you want to get the year, month, day, hour, minutes, and seconds, and you have it in a string, you can parse it with some splits, but 1 call of sscanf can do the job.

<?php
$date = ‘2007-05-18 22:15:03’;
sscanf($date,‘%d-%d-%d %d:%d:%d’,$y,$m,$d,$h,$i,$s);
var_dump($y,$m,$d,$h,$i,$s);

and thats all.

Playing with PEAR Net_IMAP

when it comes to IMAP programming, you should be very careful about IMAP commands you are issuing, an unneeded call can make your page much slower, that’s why usually IMAP Developers avoid programming with the php IMAP functions, yes they are written in C (c-client), but they don’t give you the commands you want exactly, so usually you would go for native IMAP (i.e. writing commands to a socket, and parsing the output). Continue reading Playing with PEAR Net_IMAP

Testing your code

Perhaps no other coding practice is as important as testing your code. Also in the nature of Business Development, where parts of your code always change on the request of a client (including Management), or even when you want to make your code run with better performance, Automated tests are highly needed, you can’t just spread your print statements all over your code every time you need to test it. Continue reading Testing your code