-2

Is there any reason the PHP language could not be updated in a future version to make the $ prefix on variable names optional? Reasons that would break existing code?

I'm thinking it would still be required for string interpolation, like "Hello $name", but most of the time it would be optional.

For example,

$name = 'Bob';
echo "Hello $name";

would still be valid, but so would

name = 'Bob';
echo "Hello $name";
3

The sigil for a variable in php is part of how the system works. It conveys some information for the parser about what the thing actually is. By keeping the variables and the functions in completely different name spaces, it allows one to have variable names that are function names without collision.

The without collision bit is kind of important in php, since it has been known to have quite a few standard functions. This is further complicated but he fact that parentheses for functions are optional. The parentheses are part of how other languages without sigils often separate if the identifier is a value or a function.

int foo() { return 42; }

int foo = 42;
int bar;

bar = foo;
bar = foo();

In Java, (the code above) there is no ambiguity about which foo is being used in each line because the () are required on function calls.

This is not the case in php.

$echo = "foo";
echo $echo;
echo($echo);

Both of those are perfectly valid. It is the sigil that says that one echo is a function call and the other is a variable.

So, no, the sigil for a variable cannot be made optional in php. It is part of the type system and identifier lookup and parser. It conveys necessary information that allows other things to be optional (like parentheses).

For fun, consider the following code (ideone) and the impact that sigils have on first class functions:

function greet($arg) { return "Hello $arg\n"; }

$greet = function($arg) { return "Greetings $arg\n"; };

echo $greet("Everyone");
echo greet("World");

And while there are ways of working around this in other languages that lack sigils that have first class functions, the combination of sigils and anonymous functions can be seen as making things a bit easier on the programmer and on the language designer at the same time.

  • I don't think the part about echo is that important, because user-defined functions require parentheses, so echo could be considered a keyword and, like other keywords, $ would be required on variables named echo. But points well taken about the vast existing function names and probable identical existing lambda variables. If they were made equivalent they would collide. For backwards compatibility the language could handle only lambda's that are colliding as different from function names that already exist, but that's getting pretty complicated and impractical. – David Winiecki Sep 12 '14 at 21:53
  • And now I see how my question was inviting opinionated answers. Making the $ optional without breaking backwards compatibility is technically possible depending on how you implement it, but the point at which it becomes so complicated to do so that it's a bad idea is subjective. – David Winiecki Sep 12 '14 at 21:55
  • @DavidWiniecki you might want to poke around perl some (where php inherited its $). The sigils there are much more interesting. When you dig into the typeglob you find a structure that maps foo to the scalar $foo, the array @foo, the hash %foo, the function &foo, the file handle foo, and the format foo. Which one you use depends on the context and sigil used foo. Because each of these types is in its own namespace (like php keeps functions and vars in different names), it allows some parts to be much simpler and other parts to be more expressive. – user40980 Sep 12 '14 at 22:19
  • Cool I'll check it out. :) – David Winiecki Sep 12 '14 at 22:28
  • Parentheses for functions are required in PHP. echo is not a function. – fuxia Sep 13 '14 at 19:11
3

It would probably break tons of existing code. Using the sigil means variables may use the same names as keywords, functions (built-in or user-defined), classes, etc.

  • I suppose you're correct about keywords. For functions, I think the () on the end give enough context to identify the name as a function. However I think existing lambda functions create the possibility of collisions. For classes, new and ::. – David Winiecki Sep 12 '14 at 19:00
  • However, since the sigil is already present on any variables that use keywords, it could simply be a rule that variable names that are keywords require the sigil, and non-keyword named variables do not. – David Winiecki Sep 12 '14 at 19:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.