Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

What does the function _($string) do in PHP?

I noticed this piece of code in Wordpress 2.9.1 (/wp-includes/compat.php), I don't understand it:

if ( !function_exists('_') ) {
  function _($string) {
    return $string;
  }
}

It seems that PHP indeed has a function _($string) but I can't find the documentation for it.

Upvotes: 12

Views: 4931

Answers (2)

Michael Madsen
Michael Madsen

Reputation: 55009

_ is an alias for the gettext function for translating.

gettext takes the original string as input, and finds the translation for it. This approach has the advantage that if a translation does not exist, you'll get a sensible default string out of it.

To mirror this property, the code you found essentially creates an "always failing" version of this function in case gettext isn't available.

Upvotes: 7

AJ.
AJ.

Reputation: 28184

It is an alias for gettext()

Upvotes: 10

Related Questions