-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
    Consider the code: `$echo = 'foo'; echo $echo;` ([ideone](http://ideone.com/4rq7tG)) Related (and possible dups): [Do sigils make source code easier to read?](http://programmers.stackexchange.com/q/208605/40980) and [Why is $ in identifier names for so many languages?](http://programmers.stackexchange.com/q/117943/40980). –  Sep 12 '14 at 17:42
  • http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6491#6491 – gnat Sep 12 '14 at 18:06
  • I don't think I'm soliciting opinion-based answers. I want to know specific reasons why this might not be possible. – David Winiecki Sep 12 '14 at 18:51
  • @MichaelT since the sigil is already present on any variables that use keywords, it could simply be a rule that variable names that are keywords continue to require the sigil, and non-keyword named variables do not. – David Winiecki Sep 12 '14 at 19:04
  • Examples of non-opinion based answers would be a proof that shows it is possible to make the $ optional in future PHP versions, or a counter example that shows it would break existing code. – David Winiecki Sep 12 '14 at 19:06
  • 2
    -1 for asking "Why can't the language just change their syntax to suit someone's preference?" – joshin4colours Sep 12 '14 at 19:42
  • Why is this a problem if everyone who prefers $ in all cases can continue as they always have? – David Winiecki Sep 12 '14 at 21:20

2 Answers2

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.

nobody
  • 848
  • 7
  • 12
  • 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
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. –  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
  • @toscho ... eghads ... and before reading the [documentation for echo](http://php.net/manual/en/function.echo.php) I didn't think the language could get any more idiosyncratic with arbitrary rules (and exceptions). I stand corrected. –  Sep 13 '14 at 19:49
  • @MichaelT Yeah, PHP’s “language constructs” are really weird. Even `print`, which returns a value (`1`), is not a function and therefore not `is_callable()`. :) – fuxia Sep 13 '14 at 19:58