2012-12-05 19 views
3

http://php.net/manual/en/functions.user-defined.php,我應該能夠使用下劃線(_)作爲函數名稱:使用下劃線(_)作爲PHP函數名

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

但是,下面的殺死我的程序,而不會造成任何錯誤:

error_reporting(E_ALL); 
echo('start'); 
function _($x){return ($x)?htmlspecialchars($x):'nbsp;';} 
echo('end'); 

我正在使用PHP版本5.3.18。我不明白什麼?

+0

您是否打開了錯誤報告? – eisberg

+5

使用雙下劃線代替__($ x) –

+0

上述代碼適用於我。 (PHP Version 5.3.10) – Jeremy1026

回答

12

已經有一個內置函數叫做_;它是gettext的別名。

您可能會收到Fatal error: Cannot redeclare _(),但根據您對display_errors的設置,錯誤可能不會輸出到您的屏幕。你也可以看看log_errors指令。

+0

謝謝。從來不知道。沒有很好的記錄:http://php.net/manual/en/function.gettext.php。爲什麼它不拋出某種錯誤? – user1032531

+2

@ user1032531它確實:'PHP致命錯誤:無法重新聲明_()' – Tim

+0

您必須關閉錯誤報告。我得到這個輸出:'PHP致命錯誤:不能在第3行的foo.php中重新聲明_() –

3

正如cbuckley所說,已經有一個名稱爲_gettext別名。如果您啓用了gettext,則會取得_的名稱。

您最好使用雙下劃線(__)。我經常使用它。或者你禁用gettext。