What on Earth does a double underscore then parenthesis mean in PHP? __()

php devilI was taking my first steps at really digging-below-the-surface of the code in WordPress, (trying to re-order the way my Page List was being shown on the sidebar), when I spotted a really strange piece of code in one of the examples:

(Copied April 2007 - Note: WP syntax may have changed by the time you read this!)
If you wanted to sort the list by Page Order and display the word “Prose” as the list heading (in h2 style) on a Sidebar, you could add the following code to the sidebar.php file:

<ul>
<?php
wp_list_pages(’sort_column=menu_order&title_li=<h2>’ . __(’Prose’) . ‘</h2>’ ); ?>
</ul>

Q: What the Gibbering SpiceWeasels is __(’string’) all about? Eh!

In all my 7 years of PHP coding, I had never seen such a syntax before - two underscores and then a string within parenthesis. How strange.

After a search on Google for “php __()” I arrived at a WebMasterWorld forum post where some comrade called AthlonInside had asked a similar question.

A kind soul had offered an answer (as best they could). However, according to my ’state of mind’, their answer was way off.

I was thinking its a special thing in PHP. Surely this sequence must be doing something fancy like the Heredoc syntax. The Heredoc syntax in PHP contains a sequence of three “less than signs” (”<<<”). If you’ve ever tried to search for info on it you’ll know how hard it can be!

I proceeded to do a quick scan of the PHP documentation that explains about Magic Methods. This proved to be a “wild goose chase”.

I, then, tried a different approach and searched Google again. This time I asked for “wordpress __(” and the top page in the results gave me my answer….

A: __(’string’) is just a call to a very weirdly named function in WordPress

As the info regarding Localization explains:

__($message) Searches the localization module for the translation of $message, and passes the translation to the PHP return statement. If no translation is found for $message, it just returns $message

So there we have it. A double underscore then parenthesis means nothing special in PHP. Its just another user defined function in WordPress. And quite a nifty way of dealing with multiple languages to boot!

UPDATE

I’ve just discovered that this seems to be quite a common practice for writing code for translation.

The ZEND framework translate functionality uses a similar single underscore then the string in parenthesis.

9 Responses to “What on Earth does a double underscore then parenthesis mean in PHP? __()”

  1. Eat My Business » Blog Archive » What on Earth does a single underscore then parenthesis mean in PHP? __() Says:

    […] Further to my previous post on the double underscore __() … […]

  2. Jeremy Says:

    Thank you! As someone who writes plugins and hacks the code for wordpress (from time to time as needed), it can be really hard to find someone who knows the answers to tough Wordpress questions (there are less advanced coder types than not after all).

    Your post saved me a bunch of searching, thanks!

  3. truetone Says:

    The CakePHP framework uses it too. Initially, I’d thought it was a sort of shortcut for echo or print, but interestingly enough, it’s a lot more. Thanks for the explanation.

  4. Eduardo Baldan Says:

    Thanks mate! I was trying to figure out what the @@&%* was those underscores.
    I think it should be better explained on codex.

    []’s!

  5. jeff Says:

    thanks - i couldn’t figure out what the double underscore was doing

  6. darky Says:

    I was weired, when I saw this in wordpress, too.
    But searching in google only for “php __”, the first results gave me the answer.
    But just your blog gave me the answer that is is user-defined^^
    Thx!

  7. César Salazar Says:

    Thanks! I’ve always wondered what’s with the double underscore.

  8. Anca Says:

    Wow, thank so much for this post. You’ve saved me a lot of searching.

  9. Emmanuel Says:

    Thanks too, I was contracted to write my first wordpress widget and those __(something) was really bugging me because I kept wondering if there was some obscure PHP language construct I’ve been missing for years.

Leave a Reply