In most programming languages, variables do not have identifying characters like they do in PHP. In PHP you must prefix a variable with the $
character.
Example;
var $foo = "something";
echo $foo;
I’m developing a new scripting language for a business application, and my target users do not have a programming background. Do these characters make code easier to read and use?
One reason PHP uses the $
is because without it PHP can’t tell if a name is a function reference or variable reference. This is because the language allows strange references to functions. So the $
symbol helps the parser separate the namespace.
I don’t have this problem in my parser. So my question is purely on readability and ease of use. I have coded for so many years in PHP that when I see $foo
it’s easy for me to identify this as a variable. Am I just giving a bias preference to this identifier?
18
The actual technical possibilites and limitations are nothing of the sort as are suggested throughout this thread. Let’s clear out those first.
Here’s what $ is making possible in PHP:
- Variable variables
- Variables with keyword name e.g.
$return
or being able to use same name for a variable and a functon/constant e.g.$__FILE__
Limitations or features that are unrelated to the $ prefix:
- The implementation otherwise being unable to tell difference between functions and variables
- PHP string interpolation or template syntax
- Required variable declaration
That means there is no technical reason you couldn’t have
foo = "something";
echo foo;
or
foo = "something";
echo "foo = $foo";
//would print
//foo = something
However you cannot have (assuming return
is a keyword)
return = "something";
Without serious complications. If you used a prefix like $
, then it would not
have been any problem.
It’s an opinion but I believe a sigil would be worth it for non programmers since it enables them to use keywords
as variable names, to them it would otherwise look like arbitrary limitation 😛
2
Sigils actually make a lot more sense in perl, where they provide a certain amount of type checking. In php they don’t help much outside of templating. You can get a sense of their usefulness and readability by looking around at different languages. Hardly any use them.
In an end user targeted language I’m working on, I even go further, making identifiers case insensitive and allowing spaces and apostrophes. That lets me make variable names like Karl's height
that are much closer to natural languages.
5
Years ago, I learned Applesoft Basic. Strings were always suffixed with $
and arrays had a suffix of %
. Thats just how the language worked. You looked at something, you knew what it was. I never did delve too far into the interpreter to understand why this was the case or the design decisions that made it so.
The sigil in php comes from its perl influence (which was influenced by awk
and sh
). The sigil in perl is quite a bit more than just $
as it can identify many different types:
$
scalar@
list%
hash&
codeblock*
typeglob
The sigil identifies what part of the symbol table structure you are looking at. Behind the scenes, the symbol table entry for foo (accessed via *foo
– the typeglob) has everything that may be a foo. There is $foo
, @foo
, %foo
, the format foo
, &foo
, the filehandle foo, etc…
This also allows making an alias of one variable to another:
#!/usr/bin/perl
$foo = "foo";
@qux = (1,2);
*bar = $foo;
*bar = @qux;
print "$bar @barn";
This prints foo 1 2
– in perl, this is what the sigils are really for, not that you should do this but rather that there is this behind the scenes thing that they do.
The sigils aren’t there so much for readability, but rather so that one can have $foo
and @foo
without having a collision in the namespace (compare other languages where one cannot have both int foo; int[] foo;
)
Sigils for readability are something that is learned as part of any language – reading the syntax. You could, hypothetically, enforce the type itself (as hungarian notation) to be part of the identifier.
Something in lex along the lines of:
typeChar [is]
capLetter [A-Z]
letter [a-z]
digit [0-9]
%%
{typeChar}{capLetter}(letter}|{digit})* { prientif("iddentifier");}
%%
And then you could have code like
iFoo = 42;
sFoo = "a string";
iBar = iFoo * 2;
I’m not saying this is a good idea, but rather that someone who is accustom to the language will be able to read this natively and think that it enhances readability while someone who isn’t familiar with the language may think that it just adds a bunch of noise to the language.
However, after working with a language defined this way, I could probably read it without trouble.
Some people like them, some people don’t. There are great holy wars in various forums debating this and it really boils down to how much you’ve used them.
One could design a new language for non-programmers that uses sigils and anyone who has never programed before will never complain one bit about them. On the other hand, you could not have them as part of the language and then have ruby or perl programers complain that they’re missing out on some key information.
It really doesn’t matter. What does matter is how sigils would fit into the language if you use them or not. Do you want to be able to do "123 $foo 456"
or must you do "123 " + foo + " 456"
? This is where the decision should be made.
11
I not agree that PHP uses $ to differ vars from funcs. At least because PHP have C-like syntax, and funcs() have parens after the name.
Read this post on stack overflow about why $ is in PHP.
Many popular languages, such as C, C++, C#, Java do not use $ and we can differ easy var from function.
In PHP $ helps, for example, when you write:
echo “var = $var”
Without $ such trick will be impossible.
10
After all this answers, I want give some more points to Mathew Foscarini.
- You consider problem now as a “language constructor”. You try understand why another language have this or that feature to choose if to use something in your own language. I am in same position many years, because developing SQL parser for our Valentina Database.
- I advice you take a look on antlr.org and even read book from Terence. It has a lots nice things for language developers.
- I still not agree to “reasons” exposed by other answers. They assume that PHP author in head have decide to use $ to be able use reserved keywords and Better distinguish variables from non-variables. I do not think so … although prove can be only his/their own story.
- Most probably that they just follow to perl and more early languages. As underlines Terrence, most languages are similar, especially in LEXER part. And usually constructor of a new language can just choose what kind of language he is going to develop and then take lexer of that language grammar. And this is what you should do now. No need invent from scratch. And I bet the same did PHP authors.
- All else what people mention:
- distinguish variables from non-variables
- reserver words as variable names
- ability to place variable inside of string
- may be else (I am not big expert in PHP)
are side-effects of this LEXER, because it is able to recognize a token.
Take as example: in SQL we use “” to be able to use identifiers with reserved words and even identifiers with spaces “First Name”, “Group Name”. GROUP is a keyword. There was problem – there was special solution.
P.S. Very good comment from MichaelT.
3
…a sigil allows to:
-
Better distinguish variables from non-variables. People that are still learning basic concepts might have trouble figuring out which words are variables and which don’t. They often start by reading examples or other people’s code with no adequate background.
-
Use reserved keywords or function names as variable names. At times I found some of those names to be the right ones for a variable (i.e.
$count
while there was acount()
function defined) and thanked sigils for allowing me to use them.
Also I happen to do that function name reusing frequently, for holding a function’s result in a throwaway variable, e.g.:
$isdir=isdir($dir);
if(/* complex condition implying $isdir */) {
/* etc */
}
3