The Reason For The Weird PHP Function Names
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...


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. 




Mozilla Privacy Study Vindicates Tracking Protection

Last summer Mozilla conducted a research study in which it set out to investigate how different privacy protections affect website users. A surprising finding was that having Tracking Protection enabl [ ... ]

Hash Code 2018 Registration Opens

Registration has opened for Hash Code 2018, Google's team programming competition for students and professionals in  Europe, the Middle East, and Africa. This is the fifth edition of Hash Code wh [ ... ]

More News





Last Updated ( Friday, 27 December 2013 )

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