18

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

Community
  • 1
  • 1
lala
  • 2,052
  • 6
  • 24
  • 33
  • 3
    Your closes question is exactly the same as yours and has very good answers. What do you want more? – Iznogood Aug 29 '10 at 20:33
  • I'm not entirely clear on what that person is asking but it appears to be "Why is it a $ and not some other symbol?" and others seemed to interpret it the same way according to the answers. My question, on the other hand is "What is the purpose of any symbol being there?" – lala Aug 29 '10 at 20:37
  • 1
    [This answer](http://stackoverflow.com/questions/3073812/why-php-variables-start-with-a-sign-symbol) is not satisfying? – Felix Kling Aug 29 '10 at 20:41
  • @lala well that too is adressed. From using it in "hello $name" to $ being common in char sets. Really its answered very well. – Iznogood Aug 29 '10 at 20:41
  • @Felix Kling, which one? The accepted answer? That simply says that it's because older programming languages had done it. It doesn't state the actual purpose of there being a symbol there. – lala Aug 29 '10 at 20:43
  • @Iznogood, just like those answers, you are telling me why the symbol is a $ as opposed to some other symbol. You are not telling me why PHP has a symbol in front of variables when other programming languages don't. – lala Aug 29 '10 at 20:50
  • @lala I don't see how [meder's answer](http://stackoverflow.com/questions/3073812/why-php-variables-start-with-a-sign-symbol/3073818#3073818) saying that PHP has roots in Perl, where the prefix symbol was used in a meaningful way, combined with Iznogood's example (and the similar example as an answer to the other question) of in-string variable expansion fails to adequately explain both the reason for having, and need to have, the symbol. What additional sort of explanation were you looking for? – Tim Stone Aug 29 '10 at 21:04

3 Answers3

18

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)
easel
  • 3,982
  • 26
  • 28
  • 8
    why not just use var = 'val'; $strVar = "The var is $var"; – Bogdan Mart Mar 06 '16 at 19:33
  • @BogdanMart because that would create two conventions one of which is redundant? languages should be streamlined; remember that string interpolation was one of the main things PHP was created for... (also, FWIW, I don't consider `$var` pretty and and I prefer other types of string interpolation when I can - but it certainly *can* be useful w.r.t. template strings) –  Nov 04 '17 at 22:58
  • There are surely bigger reasons than that, since JavaScript has a neat backtick string notation with dollar sign and curly braces: ```var = 'val'; strVar = `The var is ${var}`;```, which works for more than just variables. In PHP of course there is `"The var is {$var}"`, which equally shows where the var value is inserted at a glance. – s3c Mar 11 '21 at 15:20
1

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

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.

Luis
  • 313
  • 4
  • 12
  • Ok, can you honestly say that you never encountered a "undefined variable" error in PHP or where did you get the fact of variables not having to be declared in PHP? – Industrial Aug 29 '10 at 20:57
  • 1
    In PHP, a variable does not need to be declared before adding a value to it. I'm quite sure. – Luis Aug 30 '10 at 00:37
  • 3
    While it doesn't *need* to be declared, *reading* an undeclared variable does raise an `E_NOTICE: Undeclared variable`. *Writing* into a variable declares it, that's correct. – Piskvor left the building Aug 30 '10 at 06:44