24

Code:

function _()
{
    echo 'hello word';
}

Output:

Fatal error: Cannot redeclare _()

I haven't defined this function _ before, then why I am getting this error?

hakre
  • 193,403
  • 52
  • 435
  • 836
John K
  • 353
  • 4
  • 18
  • 1
    @Daniel: The topic you point out is about prefixing methods with an underscore, the OP is asking why they cannot create a function called `_`. – Jonathon Aug 21 '11 at 13:25
  • @Daniel A. White. Nope, not related at all. – phihag Aug 21 '11 at 13:25
  • The linked "dupe" is about using `_` to **prefix** method/function names not using **just** `_` alone as the function/method name. – Kev Aug 21 '11 at 13:29
  • 3
    i thought there was an amazed smiley in the title – Bojan Kogoj Aug 21 '11 at 13:34
  • possible duplicate of [What does PHP's underscore function do?](http://stackoverflow.com/questions/3413920/what-does-phps-underscore-function-do) – salathe Aug 21 '11 at 18:42

2 Answers2

31

_() is an alias for gettext.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Guys, seriously? 24 upvotes for a one-sentence answer? Could you please have a look at the answers I actually spend some time on? ;o) – deceze Aug 23 '11 at 04:24
  • Maybe you could explain exactly why on earth they made that an alias? Thats almost as bad as Javascripts $ variable. – TheLQ Aug 24 '11 at 18:43
  • @The Because I didn't have any part in the decision I can't explain *exactly* why they did it. It makes perfect sense though, since `_('Foo')` is a lot easier to work with than `gettext('Foo')` if you have a lot of l10nable strings in your app (which is quite a normal scenario). The same reason why JS libraries choose short variable names for often-used functions. – deceze Aug 24 '11 at 22:18
13

_ is an alias for gettext, a built-in function. Just as you cannot declare a function gettext, you can't create a function _ since one is already there.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • 6
    Its the standard name for function that *translates*. You can have such method in a class, for example. :P – daGrevis Aug 21 '11 at 15:13