Archived entries for PHP

Encoding and Decoding Large Numbers in PHP

I found an interesting question on StackOverflow today. Not necessarily because I have any interest in BitCoins [I don’t], but because the asker needed to convert the format of some very large numbers. In this case the number turned out to be 58 digits in base 10, and PHP has a tendency to crap out somewhere after 11 to 19 digits.

The first problem I tackled was writing a proper base58 encoding function since the ones floating around the internet seem to be spotty at best. But why limit it to just one encoding? I’ve written the same function a handful of times already, all with different, strange, and sometimes scrambled bases. I’d definitely been doing it wrong.

This is what I came up with:

function arb_encode($num, $basestr) {
	$base = strlen($basestr);
	$rep = '';

	while($num > 0) {
		$rem = $num % $base;
		$rep = $basestr[$rem] . $rep;
		$num = ($num - $rem) / $base;
	return $rep;

Continue reading…

Quick Look: Switching from PHP to Python

For the last few years it seems no one would shut up about how great Python is.

It’s easy, and intelligent, and intuitive, and so great, and it cleaned my house, did my taxes, satisfied my wife, raised my children, yadda, yadda, yadda…

Apparently this is also the language they’re teaching to new CompSci students, and even some high school students as well.

So you could understand why it was so frustrating that I couldn’t pick it up even after 2 or 3 attempts. The lack of bracket-enclosed code blocks, the ‘everything is an object with methods’ philosophy, and 3 different kinds of array turned every script I looked at into an unreadable mess, and every script I tried to write into an error-ridden monument to how much I didn’t get it.

Until 2 weeks ago, that is. I had just been hired as a PHP dev when our lead turned around and said something along the lines of “PHP is bad, all future projects will be Python-based”. Now was as good a time as any to learn. I picked up a task that was to take a simple PHP upload script with a problem [eg: PHP can’t handle file uploads over 2GB] and re-write it in Python. If you have a Python learning block I suggest you do something like this instead of half-heartedly trying “Hello World!” scripts.

Now I’m not exactly a Python pro, but here are a few tips to help you make the move. Continue reading…

HTTP Compression Self-Test in PHP

I’ve noticed that it can be difficult for many people to determine whether or not their server software is already compressing their output for them independently of what they have in their code. Most browsers do not present this information without an add-on, and Apache and IIS do not provide any information via the $_SERVER array as to whether compression is turned on, so the only way to check is to make a request against your server and inspect the response.

I’ve created the following script to simplify this process, just drop it somewhere on your server and request it with your web browser.. Continue reading…

Simple PHP PDO Wrapper Class

For the last few years I’ve been using PHP Data Objects [PDO] in all of my projects, mainly for the use of parameterized queries to avoid SQL injection. Now with PHP pushing to remove the old mysql_* functions and get the community into PDO or mySQLi I’ve found myself sharing this code more and more often. So I’m finally posting it up here so I don’t have to keep copy/pasting this after editing out all the stuff that’s specific to me.

As a bonus, this also implements the Singleton design pattern, so you can use a static Mysql_DB::getInstance() function anywhere in you code to grab the instance of the database with very little hassle.
Continue reading…

Copyright © 2009–2010. All rights reserved.

RSS Feed.