19

In PHP and some other scripting languages have the $var syntax while Java and other languages we can do just var.

Is there any theory behind it? Does it help them to parse. If not why would they choose to tack on an extra character in front?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
fastcodejava
  • 39,895
  • 28
  • 133
  • 186

6 Answers6

68

It prevents variable names conflicting with language keywords, and allows them to be interpolated in strings.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 5
    this is a nice concise answer which really doesn't help much... some examples or details would help – HorusKol Feb 13 '10 at 13:08
  • 38
    and yet... it matches flawlessly with the question – Jacco Feb 13 '10 at 13:25
  • 2
    +it's much easier to spot and work with a variable this way. – dusoft Feb 13 '10 at 13:36
  • 8
    1) In a PHP, a language with about 5700 keywords, namespace conflicts are much more common than in, say, C with about 30 keywords. 2) echo `"Hi, $first '$nick' $last, this is your $num$numext visit."` vs `"Hi, ".$first". '".$nick."' ".$last.", this is your ".$num.$numext." visit.";` - which do you prefer? – SF. Feb 15 '10 at 11:47
  • 2
    Knowing what is a keyword and what is a variable should not have to be solved with arbitrary symbols. Since there are many other languages that have managed to realize this, IMHO "preventing conflicts" is not a real reason. – Halil Özgür Feb 24 '11 at 07:34
  • 1
    @SF 5700 keywords? How did you come up with this number? The list of reserved keyword is not that long http://php.net/manual/en/reserved.keywords.php – ewernli Jan 25 '12 at 08:25
  • 1
    @ewernli: That's not counting all the builtin or included by default functions that exist in global namespace. – SF. Jan 25 '12 at 08:37
  • @Quentin really short and nice answer :) – Danish Iqbal Feb 28 '12 at 16:49
9

My theory is that scripting languages such as php would need some way to continue to run even if a new reserved word is introduced, such as php4 -> php5 got catch added. Since its a scripting language any webpages that had catch as a variable name would not die, due to the change in the language.

This is not an issue with compiled languages since everything is converted to a binary and any changes in the language would not affect already compiled programs

sinrtb
  • 99
  • 1
  • 2
    Interpreted languages like Python make do without any variable prefix. Backwards-compatibility breaking changes are held until the next major version of the language. – badp Feb 13 '10 at 13:35
  • 2
    Perl has sigils **and** is compiled. Sure it's to byte-code, but then again so is `C#` . You should really change *compiled* to *static*, since it is more accurate. – Brad Gilbert Feb 15 '10 at 14:42
8

Because constants and reserved words come without the $ thing

Don't try to compare programming languages syntaxes... They're right in being so different. :)

Earlz
  • 62,085
  • 98
  • 303
  • 499
Thiago Belem
  • 7,732
  • 5
  • 43
  • 64
8

Because its roots lay in Perl: PHP History

Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
6

Because some languages are ugly ad hoc scripting kludges and used goofy tricks to alert the "parser" to the fact that it has work to do.

And other languages were real language design efforts that used real variable names and not ugly macro syntax...

The one rather decent language that uses $ is Perl, but I might point out that Perl6 dropped it.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • 3
    The goofy tricks were probably justifiable in the 1970s when it was more important to make code easy on the compiler than easy for a human to read. – Dan Feb 15 '10 at 07:44
  • 1
    Unfortunately Perl6 did not drop sigils [1] ($, @, %). [1] http://en.wikipedia.org/wiki/Perl_6#Sigil_invariance – nimrodm Feb 15 '10 at 09:34
  • 4
    **Fortunately** Perl 6 did not drop sigils, otherwise it would no longer be Perl. – Brad Gilbert Feb 15 '10 at 14:39
  • `$array[0]` used to _get me a scalar_ from an array (`$`), but now `@array[0]` gets me a scalar _from an array_ (`@`). Why did they have to change one of the few things about good ol' Perl that made sense? – Jon Purdy Feb 17 '10 at 01:32
  • @Jon Purdy - Perl6 still makes sense, it just makes sense in a different way than Perl5 does – mob Nov 09 '11 at 22:34
1

Here is an proposed explanation as to why PHP uses $. It shows the sequence of original scripts such as awk which used the $ through Perl to PHP.

Probably originally used to make parsing of the language a lot easier.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106