61

What does the double underscores in these lines of PHP code mean?

$WPLD_Trans['Yes'] = __('Yes', $WPLD_Domain);
$WPLD_Trans['No'] = __('No', $WPLD_Domain);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drahcir
  • 951
  • 1
  • 8
  • 15
  • Given [the accepted answer](https://stackoverflow.com/questions/1777131/double-underscore-in-php/1777147#1777147), related: *[Why double underscore (__) in PHP function names?](https://stackoverflow.com/questions/5348663/why-double-underscore-in-php)* – Peter Mortensen Feb 23 '22 at 14:29

5 Answers5

70

It looks like you're using WordPress - wp-includes/l10n.php defines __ as a function that translates a string (similar to gettext and its alias, _, but with an optional parameter for explicitly specifying a domain).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SimonJ
  • 21,076
  • 1
  • 35
  • 50
  • 9
    Hideous, but very convenient. As a sidenote, php.net says that all functions starting with __ (double underscore) are reserved. It sounds like they don't like the fact that WordPress and others are poaching on their magic method territory. http://us.php.net/manual/en/language.oop5.magic.php – Lane Feb 20 '12 at 18:52
  • Based on this, it sounds like I can get rid of this if no translation is required. Is this correct? E.g., $WPLD_Trans['Yes'] = 'Yes'; – Randy Greencorn Nov 12 '14 at 17:32
42

Strictly speaking, it means nothing in PHP as it is not a pre-defined function. However, in many frameworks, like CakePHP, and other libraries the double underscore is a function used for translating strings based on the user's language/locale preference.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • 5
    No idea. Confusingly though, the CakePHP version of __ behaves completely differently to the one in Wordpress (CakePHP, by default, echoes the string unless the second parameter is false). I'll bet *that* never tripped anybody up before... – SimonJ Nov 22 '09 at 02:01
9

WordPress documents its __() function, and part of the localisation technology on Working with WordPress Core – Translate WordPress.

It is difficult to find documentation because __(), __('') or __("") is not very searchable. Double underscore and parentheses (round brackets) are some keywords to use.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PaulH
  • 2,918
  • 2
  • 15
  • 31
6

As mentioned it is generally used for translating text between languages but really it is used in the same context as any function call.

testfunction();

is no different then

__();
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 31
    Actually, testfunction() is very different, because it has a sane name. __ is a completely stupid thing to name a function. – J. Taylor Apr 13 '11 at 07:30
2

A similar or third-party GNU gettext based implementation is gettext():

Note: You may use the underscore character '_' as an alias to this function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
knoopx
  • 17,089
  • 7
  • 36
  • 41