為什麼PHP變數要以$開頭???
$$又是什麼意思?
這個問題已被關閉,原因:非技術提問的討論型問題
回複內容:
為什麼PHP變數要以$開頭???
$$又是什麼意思?
Because PHP was based on Perl which used $, though the symbols Perl used were meaningful and plenty used to indicate the data type, ( such as @ used to indicate an array ) PHP just has $
.
PHP in its early stages was a simplistic version of Perl but over time incorporated more of Perl's features, though one may argue PHP is still a simplistic primitive version of Perl since to this day most installed versions of it don't include features that have been around in other languages forever, such as closures/namespacing.
http://en.wikipedia.org/wiki/PHP
Larry Wall, the creator of Perl, was inspired to use $
from shell
scripting:
http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29
http://stackoverflow.com/questions/3073812/why-php-variables-start-with-a-sign-symbol
你說的 $$ 的上下文其實是:
$temp = 18;$age = 'temp';echo $$age;
這樣的吧
這個叫可變變數,變數名本身也作為一變數
至於你問PHP為何使用 $ 做變數的標識,那我只能說
JAVA為何要用 main 作為程式入口
PYTHON為何要用 4 個空格嚴格縮排
Go為何要fmt.Print而不是print,說實話我是真想問這個問題
這種約束成規的就從了他吧
這和你為什麼和你爸一個姓氏一樣的。。。
php的爸爸們要求它$開頭。。
那是因為開發出php的人喜歡這麼幹,任性。。。
php作者喜歡美元。
並且鍵盤方便輸入的特殊符號,其他符號有運算功能,@是郵箱,#注釋,也就_和$
方便輸入又不參與運算了。顯然$
讓人更有動力。
第一個問題 那是約定,理解為憲法就行了,不這麼幹程式不能運行
第二個是可變變數,就是把後邊變數的值作為變數的名
為什麼 PHP 使用 $
?
因為 Perl 使用 $
。
為什麼 Perl 使用 $
?
因為“變數”的英文是 scalar,首字母 S
,和 $
最接近的就是 $
了。