Double underscore in 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).
Why double underscore (__) in PHP function names?
As stated here:
PHP reserves all function names
starting with __ as magical. It is
recommended that you do not use
function names with __ in PHP unless
you want some documented magic
functionality
Long story short, PHP calls these functions implicitly and you shouldn't use this naming convention yourself.
What is the meaning of double underscore in PHP?
They are different variables. They aren't related in any special way. $foo
, $__foo__
and $__foo
are all simply different variables like $a, $b, $c
.
Functions and variables beginning with a single or double underscore
Assuming normal conventions are used in PHP:
- single underscore indicates a protected member variable or method
- double underscore indicates a private member variable or method
This stems from when PHP had weak OOP support and did not have a concept of private and protected (everything was public). This convention allowed developers to indicate a member variable or method was private or protected as to better communicate this to users of the code.
Users could choose to ignore these semantics and call the "private" and "protected" member variables and methods if so chose, though.
Underscore in php function
It means that PHP calls it implicitly.
It's called a 'Magic Method'
Also, it's two underscores, not one.
Learn more here: PHP Magic Methods
Double underscore in cakephp 2.0
The double underscore function in CakePHP handles localization. It's used when you want to translate your application by providing a string translation dictionary.
Cake PHP Book: Global Constants & Functions
See also Internationalization and Localization
What's the difference between double underscore and underscore x ?
In WordPress, both _x()
and __()
are translation functions. _x()
allows you to specify a context for the translation:
<?php _x( $text, $context, $domain ); ?>
Whereas __()
does not:
<?php __( $text, $domain ); ?>
The context option is useful for various situations where literal translation might not yield the desired result.
Since the string 'Read' on its own could have one of several different meanings in English, context is given so that translators know that they should be supplying a short term that means "Books I have read."
Some other examples could be: "date", "type", "right", "leaves", etc.
Source: WordPress Codex
Use of underscore in PHP code
There isn't any convention.
_()
(one underscore) is an alias of the PHP function gettext()
. It is used for localization.
__()
(two underscores) is a function defined by Wordpress, also for localization. You can think at it as a more flexible version of _()
but it is available only in WP projects.
Related Topics
How to Handle Error for Duplicate Entries
The Advantage/Disadvantage Between Global Variables and Function Parameters in PHP
Use Strings to Access (Potentially Large) Multidimensional Arrays
Converting Object to JSON and JSON to Object in PHP, (Library Like Gson for Java)
PHP - Remove <Img> Tag from String
How to Tell If a Session Is Active
Geo Location Based on Ip Address - PHP
Date Function Output in a Local Language
Remove Exif Data from Jpg Using PHP
How to Run the PHP Code Asynchronous
PHP Messing with HTML Charset Encoding
What Does "Mass Assignment" Mean in Laravel
PHP Get Start and End Date of a Week by Weeknumber
Dynamically Create PHP Object Based on String