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...

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


Robot Fear Of Falling - South Koreans Win DARPA Robotics Challenge
07/06/2015

It's all over! After waiting so long, we at last know how the big impressive robots did at the rescue tasks that DARPA thought up for them. And the sad news is that you aren't going to be rescued by a [ ... ]



First Peppers Sell Out
21/06/2015

SoftBank's friendly humanoid robot finally went on sale in Japan yesterday and the first batch of 1000 Peppers sold out in under a minute.


More News

 

 

 

 

Last Updated ( Friday, 27 December 2013 )
 
 

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