Category ArchivePHP



PHP 02 Feb 2018 10:02 pm

PHP Expand IPv4 to IPv6

Simple function for turning IPv6 and IPv4 into a blob that’s suitable for SQL type binary(16). This maps IPv4 to the IPv6 address space reserved for them.

function ip2binary($ip) {
	$rv = inet_pton($ip);
	if (strlen($rv) == 4) {
		// https://tools.ietf.org/html/rfc2765#section-2.1
		$rv = inet_pton('::ffff:'.$ip);
	}
	return $rv;
}

PHP 26 Apr 2012 05:00 pm

I Like PHP

While there is no doubt PHP is broken beyond repair, it does have a few virtues that keeps it as one of my first choices when writing certain kind of web frontends and minor tools.

There is a huge laundry list of how PHP is broken at “PHP a fractal of bad design“, which I almost entirely agree with. What I will do here is point out the few things on that list that I do not agree with, and why.


Continue Reading »

C &C++ &Code &Java &Perl &PHP &Python 17 Mar 2010 09:13 pm

Language Comparison: Find Longest Line

The task: Write a portable tool that takes a file name as first argument, and from that file outputs the number of lines, the longest line, the length of the longest line, and the line number of the longest line, in a binary-safe fashion (meaning, \n is the only reason a line should be delimited; embedded \0s may occur).

My sinister side goal is showing how complex the C code has to be in order to get performance near C++. It is possible to write a simpler version in C that is 20% faster for /usr/share/dict/words, but it is then 4x slower for the random files. It would also be possible to write a faster non-portable version using memory mapping, but can’t really call that simple. C++ has both readability and speed. If I missed some obvious portable optimization in the C code, let me know…

The contestants are:
[table "12" seems to be empty /]


Continue Reading »