为什么 PHP 变量以 $ 符号开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3073812/
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 08:37:38  来源:igfitidea点击:

Why PHP variables start with a $ sign symbol?

phpsyntaxvariables

提问by Bakhtiyor

Has anybody ever thought about this question. Why we must write $var_name = value;and not var_name = value;? Yes I know that it is the syntax rule that PHP uses, but why is it a $sign symbol?

有没有人想过这个问题。为什么我们必须写$var_name = value;而不是var_name = value;?是的,我知道这是 PHP 使用的语法规则,但为什么它是$符号?

回答by meder omuraliev

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

因为 PHP 是基于 Perl 的,它使用了$,虽然 Perl 使用的符号是有意义的,并且大量用于表示数据类型,(例如用于表示数组的@)PHP 只是有$.

PHP in its early stages was a simplistic version of Perl but over time incorporated more of Perl's features, though one may argue PHP was for a long time a simplistic primitive version of Perl since before PHP 5.3 it did not include features that have been around in other languages such as closures/namespacing.

PHP 在其早期阶段是 Perl 的一个简单版本,但随着时间的推移,它包含了更多 Perl 的功能,尽管有人可能会争辩说 PHP 在很长一段时间内都是 Perl 的简单原始版本,因为在 PHP 5.3 之前,它不包括已经存在的功能其他语言,例如闭包/命名空间。

http://en.wikipedia.org/wiki/PHP

http://en.wikipedia.org/wiki/PHP

Larry Wall, the creator of Perl, was inspired to use $from shell scripting: http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29

Perl 的创建者 Larry Wall 的灵感$来自 shell 脚本:http: //en.wikipedia.org/wiki/Sigil_%28computer_programming%29

回答by Cole

This has been common in computer languages for a long time, that's all. Long before Perl, too! For instance, check out Commodore 64 BASIC

长期以来,这在计算机语言中很常见,仅此而已。早在 Perl 之前!例如,查看 Commodore 64 BASIC

10 PRINT "WHAT IS YOUR NAME?"
20 INPUT A$
30 IF A$="BAHKTIYOR" THEN PRINT "HEY CHECK OUT THAT DOLLAR SIGN"

In BASIC the $ was after the variable name, however.

然而,在 BASIC 中,$ 在变量名之后。

回答by Artelius

Prepending all variables with $ makes the code somewhat easier to parse, and fits in with the "Hello $var" variable-embedded-in-string idea.

在所有变量前面加上 $ 使代码更容易解​​析,并且符合“Hello $var”变量嵌入字符串的想法。

回答by Zuul

Funny answer:

有趣的答案:

Think in PHP variables as persons, you name a person and assign it a job!

将 PHP 变量视为人,您命名一个人并为其分配工作!

But that person will refuse to work if you don't pay, so, provide a dollar in first hand :)

但是如果你不付钱,那个人会拒绝工作,所以,直接提供一美元:)

$Hyman = "drive my car" ;

$Hyman = "开我的车" ;

Just bringing fun to the "Game"! Enjoy!

只是给“游戏”带来乐趣!享受!

Regarding a real answer:

关于真正的答案:

The $ sign was chosen in early times of computer coding, because it was a sign present in virtually all char set codes, and a sign rarely needed within programming languages!

在早期的计算机编码中选择了 $ 符号,因为它是几乎所有字符集代码中都存在的符号,并且在编程语言中很少需要符号!