Has anybody ever thought about this question. Why we must write $var_name = value;
and not var_name = value;
? Yes I know that it is the syntax rule that PHP uses, but why is it a $
sign symbol?

- 795,719
- 175
- 1,089
- 1,143

- 7,198
- 15
- 55
- 77
-
2You answered your first question (it's a syntax rule)...I think you meant to ask, "Why must we write `$var_name = value;` and not `#var_name = value;` or `@var_name = value;`" ? – Justin L. Jun 19 '10 at 00:19
-
2It most likely comes from Perl, from which parts of PHP were based. – Jun 19 '10 at 00:19
-
Maybe it came from linux shell variables? – Kamil Szot Jun 19 '10 at 00:21
-
1Not the answer, but having the '$' makes it's easier to recognize something is a variable. – ggfan Jun 19 '10 at 03:13
4 Answers
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 was for a long time a simplistic primitive version of Perl since before PHP 5.3 it did not include features that have been around in other languages 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

- 8,422
- 10
- 52
- 65

- 183,342
- 71
- 393
- 434
-
1It's not the language's fault that cheap hosts don't want to upgrade. PHP is coming along as its own language nicely. – Aaron Butacov Jun 19 '10 at 00:29
-
4@Aaron - Even if you do get the latest PHP, it's still far lacking in features compared to Python/Haskell/Perl/ (insert language ). There are simply better languages out there with more features, which are more organized but just not as popular and accessible. And I'm speaking from the perspective of a person who's used PHP more than those aforementioned languages. – meder omuraliev Jun 19 '10 at 00:30
-
5
-
I believe `TRUE BASIC` also uses the dollar sign construct to indicate a preceding string variable. It would go something like `string$ = "Hello"`. It's a rather peculiar character I agree. – Franco Selem Mar 13 '14 at 17:10
-
@mederomuraliev Nice mention of haskell! Simon and JP would be proud. – Kellen Stuart Jun 23 '16 at 05:11
This has been common in computer languages for a long time, that's all. Long before Perl, too! For instance, check out Commodore 64 BASIC
10 PRINT "WHAT IS YOUR NAME?"
20 INPUT A$
30 IF A$="BAHKTIYOR" THEN PRINT "HEY CHECK OUT THAT DOLLAR SIGN"
In BASIC the $ was after the variable name, however.

- 1,483
- 1
- 11
- 20
Prepending all variables with $ makes the code somewhat easier to parse, and fits in with the "Hello $var" variable-embedded-in-string idea.

- 48,337
- 13
- 89
- 105
Funny answer:
Think in PHP variables as persons, you name a person and assign it a job!
But that person will refuse to work if you don't pay, so, provide a dollar in first hand :)
$Jack = "drive my car" ;
Just bringing fun to the "Game"! Enjoy!
Regarding a real answer:
The $ sign was chosen in early times of computer coding, because it was a sign present in virtually all char set codes, and a sign rarely needed within programming languages!
-
10
-
@Babiker, true on that, it's more or less the goal of my little fun answer! ;) – Zuul Jun 19 '10 at 01:08
-
@Babiker, good to know... I just don't know about does down votes, since my history knowledge about the dollar sign it's quiet good, and my 15 year old programming books prove it ;) – Zuul Jun 19 '10 at 01:10
-
3
-
3@John Kugelman, thats true, I did have to edit my answer, but that was a personal war that I had with my mouse ;) (sometimes I want to select a block of text to give the "comment" tag, and my mouse decides that the selected text is to be deleted... personal issues between us) ;) – Zuul Jun 19 '10 at 01:48
-
Alright, alright, uncle! You're too darn nice and cheery to downvote. – John Kugelman Jun 19 '10 at 02:30