PHP 在每个变量前面都有一个符号(美元符号)的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3596474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:21:29  来源:igfitidea点击:

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

phpvariables

提问by lala

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

可能的重复:
为什么 PHP 变量以 $ 符号开头?

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?

我看过其他编程语言,似乎它们中的大多数都没有任何符号来表明某物是变量。当其他语言的解释器/编译器能够弄清楚什么是没有这样符号的变量时,PHP 解释器是否需要这样的标志有什么原因吗?

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?

附加问题:如果有一个很好的理由来表示变量的符号,为什么不是所有的编程语言都有它?

Thisis 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?"

编辑:从评论中的混乱来看,我的问题一定是非常清楚的。澄清一下,我的问题不是“为什么变量前面的符号是 $ 而不是其他符号?”,这个问题在我链接到的页面中被问到并得到了四个很好的答案。我的问题是“为什么 PHP 中的变量前面有任何符号?在变量前面有一个符号有什么作用?”

回答by easel

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.

使用符号来表示变量使字符串插值变得简单明了。Shell、Perl 和 PHP 源于对快速简便的字符串操作(包括插值)的需求,所以我认为使用变量前缀似乎是一个好主意。

I.e. in PHP:

即在 PHP 中:

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

Compare to typical string formatting:

与典型的字符串格式相比:

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

回答by Your Common Sense

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

我认为这只是来自它的起源。
unix shell 和 Perl 就是例子。
如果你仔细观察 PHP,你会发现它与 shell 有很多共同之处。
因此,你最好在那里解决你的问题:)

回答by Luis

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.

在 php 中,您不必在使用它之前设置变量,在其他语言中,您必须在使用它之前声明变量,var MyVar = 'my value'或者定义将要保存的内容类型。

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.

我不确定,但我认为添加符号的目的是不必声明变量并让 apache 知道这是一个变量。