Archive for the ‘php’ Category

total mysql rows

Turns out our Mysql server at work is a little bigger than I thought:
Databases 75
Tables    1,549
Rows      1,018,085,348
However over the last couple of months, we’ve only averaged 130 queries/second
Hacked up PHP to gather stats:
class MysqlCounter
{
    public function __construct($host, $username, $password)
    {
        $this->conn = mysql_connect($host, $username, $password, true);
    [...]

Posted by Matthew on September 9th, 2009

Filed under mysql, php, programming | No Comments »

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)
    {
        [...]

Posted by Matthew on August 21st, 2008

Filed under php, programming | No Comments »

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 [...]

Posted by Matthew on May 1st, 2008

Filed under php, programming | No Comments »

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 {}
    function register_object($obj)
    {
        //  Check for [...]

Posted by Matthew on April 15th, 2008

Filed under php, programming | 2 Comments »

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 you have.

Posted by Matthew on April 2nd, 2008

Filed under php, programming | No Comments »

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
        $lines = array_map(’trim’, $lines);           [...]

Posted by Matthew on March 28th, 2008

Filed under php, programming | 1 Comment »

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 [...]

Posted by Matthew on February 12th, 2008

Filed under php, programming | No Comments »

Awful code I’ve written today

$row = $table->tr();

if(!($amIAltOrNot = !$amIAltOrNot))
{
    $row->class = "alt";
}

//  Edit: Much better now
$row = $table->tr();

$row->class = ($amIAltOrNot = !$amIAltOrNot) ? "" : "alt";

Posted by Matthew on January 29th, 2008

Filed under php, programming | No Comments »

Now with added Slicehost

This website is now hosted on Slicehost (along with section99.net). It’s a little early to know how it will turn out, but so far I’m loving having not only a shell, but root access.
Signing up was completely painless and the the VPS was provisioned within minutes with several different operating system options (though [...]

Posted by Matthew on January 26th, 2008

Filed under linux, mysql, php, website | No Comments »

More useless PHP – Inplace Reverse

Code
<?php

/*

    PHP implementation of a common programming problem:

        Reversing a singly linked list

*/

    error_reporting(E_ALL | E_STRICT);

    //  This is our basic node
    class WordNode
    {
        public $word;
        public $nextNode;

        public function __construct($word = null)
        {
  [...]

Posted by Matthew on January 23rd, 2008

Filed under php, programming | 1 Comment »