PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Bits and PHPieces

Note: This article was originally published at Planet PHP on 7 December 2010.
Planet PHP

I like to think of PHP as a mixed bag of tricks. The language itself was born out of some simple, real-world use cases, and that ideology still drives the language forward today. It sometimes isn't pretty, but PHP isn't meant to be prettya-ait's meant to solve problems.

One (some would say unfortunate) side-effect of this ideology is that there are quite a few things in the PHP language that exist only to solve one particular, and sometimes obscure, problem. Sure, the addition of an object model in PHP 4 (later rewritten in PHP 5) and the addition of closures and lambda functions in PHP 5.3 both constituted welcome additions to the language. Even the often-derided goto, feared by many a developer, is a general-purpose operator that has a relatively broad area of usefulness. But, what about some of the more obscure things that exist in PHP, or the multitude of extensions?

Down the rabbit hole

My favorite example of obscure functionality is embodied in tick functions and the declare construct. If you've never used these before, don't worry; you're not alone. What are they, you ask? Thankfully, the PHP documentation team has done a wonderful job describing what this oddity actually does.

aoA tick is an event that occurs for every N low-level tickable statements executed by the parser within the declare block. The value for N is specified using ticks=N within the declare blocks's directive section.a

So, it looks like this is meant to allow an arbitrary function to be executed aoevery N low-level tickable statements executed by the parser.a This, of course, comes with some pretty serious overhead; you're basically telling PHP to execute a bunch of opcodes, stop, do whatever this tick function is supposed to do, and then resume.

Now, before you go out and write tick functions for profiling your apps (which is the first thing that comes to mind for most people), let me remind you that this functionality was introduced in the early days of PHP 4, and has generally not seen much use out in the wild; the highly inefficient nature of tick functions coupled with the fact that most of their usefulness has been superseded by more efficient and special-purpose solutions (e.g., Xdebug for profiling and debugging). Don't be surprised if tick functions get removed from PHP at some point in the future, either. Considering most developers don't even know the exist, I don't believe they'll be missed.

Curiouser and curiouser

PHP has a lot of built-in functionality for manipulating and searching strings, which isn't surprising, considering that most web apps consist of fancy ways to display text-based data collected through html forms. Most developers will feel right at home with functions like htmlentities() and str_replace(), but what about metaphone() or soundex() to write a spell checker? Or, hebrev() to convert aological Hebrew text to visual text.a And, there's always my favorite string function, str_rot13(), which brings me back to my forum-trolling days when movie spoilers and the like were ROT13ed to ensure that casual readers wouldn't mistakenly read the synopsis for last night's episode of Friends.

Yet another of my favorite, lesser-known functions is token_get_all(). Now, if you're familiar with some basic compiler theory and know what a lexer is, then this method is hardly surprising. For those unfamiliar with these concepts, don't freta-athe functionality of token_get_all() can be easily demonstrated with a simple example:

'); /* $tokens = array(array(368, '

We can see here that token_get_all() takes a string representing a PHP script as its only argument, and returns an array of so-called aotoken identifiers.a Now I don't know about y

Truncated by Planet PHP, read more at the original (another 2064 bytes)