Programming archaeology: PHP's dollar sigil

15th March 2017

I started out with Java as my professional language and before I discovered Ruby, PHP was my main programming language of choice for new projects. This was probably six to seven years ago or more. I had some pretty good times with CodeIgniter [0] and various libraries like HHMVC [1].

In any case, I digress - even back then the most striking feature of its syntax was the dollar sign prepended to variables which made me think the aesthetics of the language were sub-par or at least annoying. Clearly it did not stop me for writing lots of apps in PHP since it was more of a small annoyance, if you will, something debatable mostly for scholastic purposes.

Recently the question popped into my mind: "What's the history of the dollar sign? and why is there." - so like every good question I started to Google my way into some answers:

First influence - Perl:

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 $.

Then Quora provided a very nice anecdote, collating the above results even more:

PHP was originally implemented as a set of Perl scripts written by Rasmus to manage his website -- "PHP" stood for "Personal Homepage". A number of other syntactical elements in PHP are reminiscent of Perl as well.

Perl probably picked up this from awk and sh, which likely picked it up from BASIC, which predates Unix by about 5-10 years.

From this we find out that $, @ and % are actually called sigils [3] in computer programming. Next stop was to see what these sigils indicated in Perl:

Scalar values are always named with '$', even when referring to a scalar that is part of an array or a hash. The '$' symbol works semantically like the English word "the" in that it indicates a single value is expected.

Very interesting and they remind me of Ruby's shorthand or general delimited input:

%w(foo bar)# => ["foo", "bar"]# %r() is another way to write a regular expression.# %q() is another way to write a single-quoted string (and can be multi-line, which is useful)# %Q() gives a double-quoted string# %x() is a shell command# %i() gives an array of symbols (Ruby >= 2.0.0)# %s() turns foo into a symbol (:foo)

Conclusion? the dollar sign for variables in PHP is actually a vestigial sigil since compared to Perl there aren't any for arrays and hashes.