Homepage of this site

Dented Reality Notes

19 Apr 2005

Nerdalicious Sub-Conscious PHP Debugging (#)

I've dreamt in binary before, no big deal. When I was learning it (properly) and working with it a bit at university, I got a little carried away and would write my name on things in binary, that sort of ultra-cool nerd thing. Now I've stepped up from binary to PHP, and things appear to have accelerated from there;

Last night, I de-bugged the code that I was writing before I went to bed, in my sleep! I was working on my soon-to-be-released PHP Atom API package last night, as has been the case on many nights recently, and it got to be pretty late. I showered up, went to bed and all was well.

When I woke up this morning - I had memories of dreaming my way through the code that I had been working on the night before (1,500 lines, so no small feat ;) ), and in my dream, I had found a style of catching errors on some of my processing which wouldn't actually catch anything, and would always evaluate as if there was no error. I got up this morning and checked it, and sure enough - I was more coherent in my dream than I was while sitting in front of my PC coding.

My friend Ray dubbed this "nerdalicious" and I think that's a pretty awesomely descriptive word for it, so I'm calling this 'nerdalicious sub-conscious code de-bugging'.

[Posted @ 13:12, in: /notes/web/php | Comments Disabled!]

14 Apr 2005

Better Random Hex (#)

Eric Scheid (founder of the excellent IAWiki) shot me a line with a more efficient and much sleeker version of my random hex code straight after I posted it, so here it is!

function random_hex() {
        $color = '';
        for ($c = 0; $c < 6; $c++) {
                $i = rand(0, 15);
                $color .= substr("0123456789ABCDEF", $i, 1)
        }
        $color = '#' . $color;
        return $color;
}

Thanks Eric!

[Posted @ 21:54, in: /notes/web/php | Comments Disabled!]

11 Apr 2005

Random Hex (#)

I needed some code to randomly generate me a hex value for use as a color in a webpage. Here's what I came up with (WARNING: Some colors are UGLY!)

function random_hex() {
	$color = '';
	for ($c = 0; $c < 6; $c++) {
		$i = rand(0, 15);
		switch ($i) {
			case 10 :
				$i = 'A';
				break;
			case 11 :
				$i = 'B';
				break;
			case 12 :
				$i = 'C';
				break;
			case 13 :
				$i = 'D';
				break;
			case 14 :
				$i = 'E';
				break;
			case 15 :
				$i = 'F';
				break;
			default :
				$i = $i;
				break;
		}
		$color .= $i;
	}
	$color = '#' . $color;
	return $color;
}

[Posted @ 15:13, in: /notes/web/php | Comments Disabled!]

05 Feb 2004

File Append Function (#)

Here's a useful function that I wrote for PHP - it just opens a specified file and appends a string to it. It's very good for logging things.

<?php
/**
 * @return boolean
 * @param $file FileNameToWriteTo
 * @param $string StringToWriteToFile
 * @desc Writes specified string to the
         end of the file with a linefeed attached
*/
function file_append($file, $string) {
	if (is_file($file) && is_writable($file)) {
		$fh = fopen($file, 'a');
		if ($fh) {
			fwrite($fh, $string . "\n");
			fclose($fh);

			return true;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}
?>

You might want to consider making the fopen() flags 'ab' for binary-safe (now reccommended on php.net) and also changing the '\n' part to the appropriate line-endings for your operating system (\n = *NIX, \r\n = Windows, \r = Mac).

[Posted @ 17:09, in: /notes/web/php | Comments Disabled!]

Browse All Posts By Date

August 2008

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31