热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么PHP在变量前面有$符号?-WhydoesPHPhavea$signinfrontofvariables?

InPHPandsomeotherscriptinglanguageshavethe$varsyntaxwhileJavaandotherlanguageswecan

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

在PHP和其他一些脚本语言中有$ var语法,而Java和其他语言我们可以做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?

它背后有什么理论吗?它能帮助他们解析吗?如果不是为什么他们会选择在前面加上一个额外的角色呢?

6 个解决方案

#1


67  

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

它可以防止变量名与语言关键字冲突,并允许它们以字符串形式进行插值。

#2


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.

我的理论是,像php这样的脚本语言需要某种方式继续运行,即使引入了一个新的保留字,例如php4 - > php5 got catch。由于它是一种脚本语言,任何被捕获为变量名称的网页都不会因为语言的变化而死亡。

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

这不是编译语言的问题,因为所有内容都转换为二进制文件,语言中的任何更改都不会影响已编译的程序

#3


8  

Because constants and reserved words come without the $ thing

因为常量和保留字没有$ thing

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

不要试图比较编程语言的语法......他们是如此不同。 :)

#4


8  

Because its roots lay in Perl: PHP History

因为它的根源在于Perl:PHP历史

#5


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.

因为有些语言是丑陋的特殊脚本kludges并使用愚蠢的技巧来提醒“解析器”它有工作要做的事实。

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.

使用$的一种相当不错的语言是Perl,但我可能会指出Perl6掉了它。

#6


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.

以下是PHP使用$的原因的建议解释。它显示了原始脚本的序列,例如awk,它使用$ Perl到PHP。

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

可能最初用于使语言的解析更容易。


推荐阅读
author-avatar
journeylis-1998_246
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有