PHP 在每个变量前面都有一个符号(美元符号)的目的是什么? [英] What is the purpose of PHP having a symbol (the dollar sign) in front of each variable?

查看:47
本文介绍了PHP 在每个变量前面都有一个符号(美元符号)的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我看过其他编程语言,似乎它们中的大多数都没有任何符号来表明某物是变量.当其他语言的解释器/编译器能够弄清楚什么是没有这样符号的变量时,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?

它会让解释器更快吗?是否让工程师更容易创建解释器?是为了让代码更容易阅读吗?还是其他什么原因?

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.

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

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

推荐答案

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

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.

即在 PHP 中:

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

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

Compare to typical string formatting:

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

这篇关于PHP 在每个变量前面都有一个符号(美元符号)的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆