Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

variables

php

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

like image 763
lala Avatar asked Aug 29 '10 20:08

lala


People also ask

What does the dollar sign mean in PHP?

$ is the way to refer to variables in PHP. Variables in PHP are dynamically typed, which means that their type is determined by what's assigned to them. Here's the page about variables from the PHP manual. $a = "This is a string"; $b = 1; // This is an int.

Why is the dollar sign used for variables?

$ is used to DISTINGUISH between common variables and jquery variables in case of normal variables.

What is the use of the symbol in PHP?

The at sign (@) is used as error control operator in PHP. When an expression is prepended with the @ sign, error messages that might be generated by that expression will be ignored. If the track_errors feature is enabled, an error message generated by the expression and it will be saved in the variable $php_errormsg.

What does a dollar sign mean in code?

That dollar sign means: we're in the system shell, i.e the program that you're put into as soon as you open the Terminal app. The dollar sign is often the symbol used to signify where you can begin typing in commands (you should see a blinking cursor there).


2 Answers

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)
like image 136
easel Avatar answered Oct 12 '22 09:10

easel


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

like image 33
Your Common Sense Avatar answered Oct 12 '22 11:10

Your Common Sense