Archive for the ‘programming’ Category
i hate working with dates
This little snippet gets all the days in a month, and groups them by week. <?php error_reporting(E_ALL); // Return an array of all the days in a month grouped by week number // (Sunday is considered to be the first day of the week) function weeksInMonth($month, $year) [...]
Extracing links from HTML using PHP
Many months ago there was a PHP competition to make the smallest script to extract all the links from a document. I’ve lost a link to the actual site, but the rules and conditions were set up expecting everyone to solve the problem with regular expressions. In my opinion relying on regular expressions to parse [...]
Simple type checking in PHP
<?php error_reporting(E_ALL | E_STRICT); /* Manual optional type checking for PHP functions Basic example: function log_error($line_number, $filename, $desc) { CheckFunctionArgs(‘integer’, ‘string’, ‘string’); [snip] } Object example: class LogObject {} [...]
sizeof(int) = 68
Pankaj Kumar has a slightly disturbing look at memory usage in PHP. Each element requires a value structure (zval) which takes 16 bytes. Also requires a hash bucket – which takes 36 bytes. That gives 52 bytes per value. Memory allocation headers take another 8 bytes*2 – which gives 68 bytes. Pretty close to what [...]
fun with anagrams
<?php define(’WORD_LIST_FILENAME’, ‘/usr/share/dict/words’); class AnagramLookup { private $lookup; // Loads a file with one word per line private function load_word_list($filename) { $lines = file($filename); // One word per line [...]
phpt testing framework
PHPT is the kind framework that encourages testing simply by making everything so easy. All that’s needed is a file with your PHP code and expected output. It wont replace SimpleTest or PhpUnit for anything complicated (say, like PHP itself…) but it seems to be just what I’m after. There’s little documentation about (PHP QA [...]
