13

Is it possible to name variables in a Java-like manner in PHP, such as by removing the need for a $ sign each time? If so, how can I enable the setting which does this?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 6
    Can you explain why you'd want to do this? – ceejayoz Apr 06 '09 at 21:16
  • 2
    It may look weird at first, but it's actually quite a useful feature. – troelskn Apr 06 '09 at 21:26
  • Just sick of typing $ signs all the time, java and other languages are great in terms of this. How is it a useful feature troelskn? – Ali Apr 06 '09 at 21:42
  • 4
    The $-sign is usefull in a way that you can see what is a var, and what is not in a split second. – Pim Jager Apr 06 '09 at 23:07
  • everything is a var imo, constants are barely ever used – Ali Apr 07 '09 at 09:19
  • 1
    Y'know what $ sigils do for you: you never have to care, or even think, about the possibility that your variable name might conflict with a syntactic token. – chaos Apr 10 '09 at 04:20
  • With a good IDE you find out instantly if that happens! – Ali Apr 10 '09 at 07:39
  • 4
    Nothing I hate more than a language misfeature that people justify because "oh, it's okay because any good IDE will make that a non-issue". People do that with PHP's pathetic failure to catch simple variable name typos until runtime, and it's unacceptable. – chaos Apr 10 '09 at 17:46
  • 2
    Variables should be extremely common, so why do you have to modify the most common with a $? Shouldn't something else get the $? – akway Jul 22 '09 at 07:15
  • @shinzou Thanks, this nine year old thread really needed a pissy reply. – ceejayoz Feb 06 '18 at 23:29
  • Its age is irrelevant since php is still being used and this dollar sign neurosis is still being justified. @ceejayoz – shinzou Feb 08 '18 at 09:39

4 Answers4

11

Sorry, it's not possible. The closest you'll get are constants:

define('CONS', 5);
echo CONS;
Peter
  • 1,316
  • 8
  • 4
  • 7
    He clearly said "variables", which isn't "close" to constants in any real sense at all. – Rob Jul 19 '09 at 01:18
  • 7
    It is quite the opposite in fact. – akway Jul 22 '09 at 07:17
  • Maybe in PHP; that's why Tcl is better, because its constants can vary. :) – abarnert Aug 12 '14 at 06:59
  • 1
    @Rob you can use a constant array with one element - the variable *chuckle* –  Nov 04 '17 at 22:17
  • What about using magic methods? Ie: __get and __set - Would this make having a variable name without a dollar sign possible? Or would it at least make it appear so from outside of the class? – Kevin Wheeler Jul 05 '22 at 00:42
  • In Laravel, you are supposed to use syntax like this often: $this->faker->name or User::first()->user ... I think it's implemented using magic methods under the hood, but would like confirmation. – Kevin Wheeler Jul 05 '22 at 00:45
9

I trust the other answers in that this must be impossible.

While I personally hate PHP, everybody has some good characteristics. One reason the PHP $ is very nice has to do with variable interpolation:

$bob = "rabbit"
$joe = "dragon-$bob"  // ==> dragon-rabbit

That's pretty nice and short. In Ruby, since variables do not have to have any particular starting character, you have to type a bit more:

bob = "rabbit"
joe = "dragon-#{bob}"

And the same thing happens in Java (well, I left Java when you still had to use either StringBuffer or concatenate with " + bob + "... I'm sure that's gone by now).

So the dollar sign is annoying and ugly, but here's at least one advantage (there must be more).

At the same time, if you try to write really nice PHP code (in spite of what you see around), you will start to see your code as elegant, and those dollar signs will be symbolic for... money!

chaos
  • 122,029
  • 33
  • 303
  • 309
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • Good point. I think you still need to use + signs in Java, someone correct me if I'm wrong :) – Ali Jul 18 '09 at 18:17
  • Groovy has variable interpolation. ;) – Rob Jul 19 '09 at 01:20
  • I think you're right. Note that there are some configuration options that have nothing to do with this, but will help. One of the most used is the one that lets you use =$what?> instead of using echo everyplace. Check out the php.ini stuff... – Dan Rosenstark Jul 19 '09 at 01:21
  • @Rob: thanks for that, and for telling me it's called "variable interpolation." Also see this http://www.koders.com/java/fid39C2E8453EEC56A6A2A9DFC5FFE7F6E4F7066527.aspx – Dan Rosenstark Jul 19 '09 at 01:26
  • I disagree. If you count it up, the ruby version is actually the same number of characters. It also could be written (in Ruby): joe = "dragon-" + bob which is even more clear. And besides, having to put #, {, and } 1% of the time is better than having to put $ 99% of the time. – akway Jul 22 '09 at 07:21
  • Dear Akway: You are correct, but it's nice to get interpolation for free in PHP, though your code is longer for it and it's annoying except for interpolation. I should mention that I'm a strong believer in The IDE (as a concept), so all of this is a non-starter for me. Netbeans actually types the #{} for me and puts my cursor in the center. In PHP I put the dollar and the var lists pop up. And in Java, because you've got static types, the IDE basically writes all the code for you, but then it's really long because you didn't use Ruby :) – Dan Rosenstark Jul 22 '09 at 09:33
  • There are plenty of languages that use shell-style (`$bob`) interpolation but don't require you to tag your variables with `$`. There's no reason at all the two have to go together. – abarnert Aug 12 '14 at 07:00
  • @abarnert good point. I hadn't realize that they're not necessarily tied. – Dan Rosenstark Dec 16 '16 at 15:37
7

Nope. Variables in PHP must start with $.

The other approach is to use constants.

chaos
  • 122,029
  • 33
  • 303
  • 309
  • 3
    Constants aren't "another approach" to variables if you need to modify the contents of the variable. – Rob Jul 19 '09 at 01:18
  • @Rob you can use a constant array with one element - the variable *chuckle* –  Nov 04 '17 at 22:17
4

It's like asking Java to be non-strong-typed:

str = "some string";
System.out.print(str.contains("some"));

Makes no sense whatsoever.

chaos
  • 122,029
  • 33
  • 303
  • 309
karim79
  • 339,989
  • 67
  • 413
  • 406