The Reason For The Weird PHP Function Names
Written by Ian Elliot   
Friday, 27 December 2013

This could just as well be in the humour section. Do you know why PHP has such strange legacy function names? You might be more than surprised when you find out...

phplogo 

PHP Manual Master Pieces is a web site that lists

"Sparkling gems found in the official PHP documentation"

It is a really good read, but only if you are into PHP and some of the wonderful suggestions found in the PHP documentation and the user submitted comments in particular. 

Although it is of interest to mainly PHP programmers, a recent post has enough shock value to be spread to a wider audience who can either feel sorry for the PHP-ers or can laugh themselves silly in a bath of schadenfreude. 

The post on the PHP documentation php.internals forum starts with an innocent enough question. Why are the PHP functions not named using a consistent style that makes them, if not logical, much more regular? The answer supplied is a little bit of PHP history:

"Well, there were other factors in play there. htmlspecialchars was a very early function. Back when PHP had less than 100 functions and the function hashing mechanism was strlen(). In order to get a nice hash distribution of function names across the various function name lengths names were picked specifically to make them fit into a specific length bucket. This was circa late 1994 when PHP was a tool just for my own personal use and I wasn't too worried about not being able to remember the few function names."

OK, you might not have followed what is going on. It isn't so much obscure as unbelievable. Most languages use a hash table to store symbols and this is fairly standard, but in the early days PHP had few functions and just to get things to work the hash function used was string length. Yes, that is correct. All of the functions with the same number of characters in their name were hashed to the same storage location. So obviously to make the system more efficient the function names had to be picked to ensure that the hash function scattered them evenly over the storage - to be 100% clear this means that function names weren't chosen on the grounds of clarity or for consistency of style but to ensure a good distribution of string lengths. 

This is a case of the hash function wagging the data....

Of course this was 20 years ago and no language designer or implementer in their right mind would use such a poor hash function and then arrange for the data to turn it into a good hash function at the expense of linguistic clarity - or would they? 

My guess is that our mistakes grow more subtle over time. 

 

phplogo

Banner


Firefox OS Comes To Raspberry Pi
28/10/2014

Firefox OS hasn't taken over the world yet despite its promise. Now we have a small effort from Mozilla to make it run on one of the most successful budget devices in the world - the Raspberry Pi.



Azure Plugin For Android Studio And IntelliJ
04/11/2014

Google owns Android, but that doesn't stop Microsoft from trying to get Android apps to make use of Azure and Office 365. Does the new plugin make a Microsoft-flavoured Lollipop any more likely?


More News

 

 

 

 

Last Updated ( Friday, 27 December 2013 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.