开发者

What is the purpose of PHP having a symbol (the dollar sign) in front of each variable? [duplicate]

This question already has answers here: Closed 12 years ago.

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, althoug开发者_开发百科h 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?"


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.

I.e. in PHP:

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

Compare to typical string formatting:

var = 'val'
strVal = 'The var is %s' %(var)


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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜