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

More Information

PHP Manual Masterpieces

PHP Internals

Related Articles

New - PhpStorm 7       

HipHop VM - Facebook's PHP Virtual Machine    

PHP 5.5.0 Released       

 

Zend Optimizer+ For PHP?

 

PHP 5.5 Beta1 Released        

 

Zend Survey of PHP Developers       

  

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

Banner


Pi Day - The Great Unanswered Questions
14/03/2024

It's Pi day again, again, again... Even after so many, I still have things to say about this most intriguing number. The most important things about Pi is that it is irrational and one of the few tran [ ... ]



Azure AI And Pgvector Run Generative AI Directly On Postgres
26/03/2024

It's a match made in heaven. The Azure AI extension enables the database to call into various Azure AI services like Azure OpenAI. Combined with pgvector you can go far beyond full text search. Let's  [ ... ]


More News

 

 

 

 

Last Updated ( Friday, 27 December 2013 )