Programming archaeology: PHP's dollar sigil

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

via http://stackoverflow.com/a/3073818

Perl might have this from shell tools like awk et al. hence:

Having a symbol to denote variables makes string interpolation simple and clear. Shell, Perl and PHP grew out of the need for quick and easy string manipulation

via http://stackoverflow.com/a/3596504

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.

Bit of trivia: the $ (or @ or % in Perl 5) is referred to as a sigil.

via https://www.quora.com/Why-we-use-dollar-symbol-before-variables-in-PHP

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.

  $days               # the simple scalar value "days"
  $days[28]           # the 29th element of array @days
  $days{'Feb'}        # the 'Feb' value from hash %days
  $#days              # the last index of array @days

Entire arrays (and slices of arrays and hashes) are denoted by '@', which works much as the word "these" or "those" does in English, in that it indicates multiple values are expected.

  @days               # ($days[0], $days[1],... $days[n])
  @days[3,4,5]        # same as ($days[3],$days[4],$days[5])
  @days{'a','c'}      # same as ($days{'a'},$days{'c'})

Entire hashes are denoted by '%':

  %days               # (key1, val1, key2, val2 ...)

via http://search.cpan.org/~shay/perl-5.24.1/pod/perldata.pod#Variable_names

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.

[0] - https://codeigniter.com
[1] - https://github.com/jenssegers/codeigniter-hmvc-modules
[3] - https://en.wikipedia.org/wiki/Sigil_(computer_programming)

Tagged under: