views:

104

answers:

4

Possible Duplicate:
Why PHP variables start with a $ sign symbol?

I have looked at other programming languages and it seems that most of them do not have any symbol to show that something is a variable. Is there some reason why a PHP interpreter needs such a sign, when interpreters/compilers for other languages are capable of figuring out what is a variable without such a symbol?

Does it make it faster for the interpreter? Does it make it easier for engineers to create an interpreter? Is it to make the code easier to read? Or some other reason?

Bonus question: And if there is a good reason to have a symbol connoting a variable, why don't all programming languages have it?

This is the closest question I could find, although the question seems unclear and the answers range from "just because" to "here's why it's a $ and not some other symbol." That thread did not seem to address the actual purpose of the dollar sign.

EDIT: My question must have been horribly articulated, judging from the confusion in the comments. To clarify, my question is not "Why is the symbol in front of a variable a $ as opposed to some other symbol?", a question that was asked and got four good answers in the page I linked to. My question is "Why is there any symbol at all in front of a variable in PHP? What purpose does it serve to have a symbol in front of a variable?"

+3  A: 

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, including interpolation, so I imagine using a variable prefix seemed like a good idea.

IE:

$var = 'val';
$strVar = "The var is $var";

Compare to:

var = 'val'
strVal = 'The var is %s' %(var)
easel
+3  A: 

PHP is compiled every time a webpage is loaded. This means it needs to be very quick to parse (parsing is the slowest part of the compilation process), and adding the dollar sign speeds up parsing as it makes identifying identifiers that much easier (especially in strings).

fredley
A: 

In php you don't have to set the variables prior to using it, in other languages you have to declare variables before using it like var MyVar = 'my value' or defining what kind of content is going to hold.

I'm not sure but I think the purpose of adding a symbol was to not have to declare variables and let apache know that this is a variable.

Luis
Ok, can you honestly say that you never encountered a "undefined variable" error in PHP or where did you get the fact of variables not having to be declared in PHP?
Industrial
In PHP, a variable does not need to be declared before adding a value to it. I'm quite sure.
Luis
@Luis: While it doesn't *need* to be declared, *reading* an undeclared variable does raise an `E_NOTICE: Undeclared variable`. *Writing* into a variable declares it, that's correct.
Piskvor
A: 

I think it's just from it's origins.
unix shell and Perl were examples.
if you watch PHP closer you will see very much in common with shell.
Thus, you'd better address your question there :)

Col. Shrapnel