Subscribe to PHP Freaks RSS

There’s a Gender Extension for PHP

syndicated from www.sitepoint.com on June 26, 2017

Unlike in our "mainstream" paid course about exploring PHP, I like to explore the weird and forgotten areas of the language.

Recently, I ventured into a section of the PHP manual which lists extensions that are used to help with Human Language and Character Encoding. I had never looked at them as a whole - while dealing with gettext, for example, I always kind of landed directly on it and ignored the rest. Well, of those others, there's one that caught my eye - especially in this day and age given the various controversies - the Gender extension.

Pink and green elephant symbolizing gender roles

This extension, in short, tries to guess the gender of first names. As its introduction says:

Gender PHP extension is a port of the gender.c program originally written by Joerg Michael. The main purpose is to find out the gender of firstnames. The current database contains >40000 firstnames from 54 countries.

This is interesting beyond the fact that the author is kinda called George Michael. In fact, there are many aspects of this extension that are quite baffling.

While its last stable release was in 2015, the extension uses namespaces which clearly indicates that it's not some kind of long lost remnant of the past - a relatively recent effort was made to make it conform to modern coding standards. Even the example code uses namespaces:

<?php

namespace Gender;

$gender = new Gender;

$name = "Milene"; $country = Gender::FRANCE;

$result = $gender->get($name, $country); $data = $gender->country($country);

switch($result) { case Gender::IS_FEMALE: printf("The name %s is female in %s\n", $name, $data['country']); break;

case Gender::IS_MOSTLY_FEMALE: printf("The name %s is mostly female in %s\n", $name, $data['country']); break;

case Gender::IS_MALE: printf("The name %s is male in %s\n", $name, $data['country']); break;

case Gender::IS_MOSTLY_MALE: printf("The name %s is mostly male in %s\n", $name, $data['country']); break;

case Gender::IS_UNISEX_NAME: printf("The name %s is unisex in %s\n", $name, $data['country']); break;

case Gender::IS_A_COUPLE: printf("The name %s is both male and female in %s\n", $name, $data['country']); break;

case Gender::NAME_NOT_FOUND: printf("The name %s was not found for %s\n", $name, $data['country']); break;

case Gender::ERROR_IN_NAME: echo "There is an error in the given name!\n"; break;

default: echo "An error occurred!\n"; break;

}

While we have this code here, let's take a look at it.

Continue reading %There’s a Gender Extension for PHP%