Double underscore in PHP

PhpSyntax

Php Problem Overview


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

$WPLD_Trans['Yes'] = __('Yes', $WPLD_Domain);
$WPLD_Trans['No'] = __('No', $WPLD_Domain);

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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

__();

Solution 5 - Php

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrahcirView Question on Stackoverflow
Solution 1 - PhpSimonJView Answer on Stackoverflow
Solution 2 - PhpMike BView Answer on Stackoverflow
Solution 3 - PhpPaulHView Answer on Stackoverflow
Solution 4 - PhpJasonDavisView Answer on Stackoverflow
Solution 5 - PhpknoopxView Answer on Stackoverflow