Can PHP be used without the dollar sign $ symbol for variables?
Asked Answered
F

4

14

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?

Fayalite answered 6/4, 2009 at 19:55 Comment(11)
Can you explain why you'd want to do this?Mannos
It may look weird at first, but it's actually quite a useful feature.Neuropsychiatry
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?Fayalite
The $-sign is usefull in a way that you can see what is a var, and what is not in a split second.Dejadeject
everything is a var imo, constants are barely ever usedFayalite
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.Chestnut
With a good IDE you find out instantly if that happens!Fayalite
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.Chestnut
Variables should be extremely common, so why do you have to modify the most common with a $? Shouldn't something else get the $?Meneau
@shinzou Thanks, this nine year old thread really needed a pissy reply.Mannos
Its age is irrelevant since php is still being used and this dollar sign neurosis is still being justified. @MannosRorie
G
11

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

define('CONS', 5);
echo CONS;
Guilty answered 6/4, 2009 at 19:58 Comment(6)
He clearly said "variables", which isn't "close" to constants in any real sense at all.Cenozoic
It is quite the opposite in fact.Meneau
Maybe in PHP; that's why Tcl is better, because its constants can vary. :)Acolyte
@Cenozoic you can use a constant array with one element - the variable chucklePrimatology
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?Studied
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.Studied
K
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!

Keeler answered 18/7, 2009 at 16:54 Comment(8)
Good point. I think you still need to use + signs in Java, someone correct me if I'm wrong :)Fayalite
Groovy has variable interpolation. ;)Cenozoic
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...Keeler
@Rob: thanks for that, and for telling me it's called "variable interpolation." Also see this koders.com/java/…Keeler
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.Meneau
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 :)Keeler
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.Acolyte
@Acolyte good point. I hadn't realize that they're not necessarily tied.Keeler
S
7

Nope. Variables in PHP must start with $.

The other approach is to use constants.

Socage answered 6/4, 2009 at 19:58 Comment(2)
Constants aren't "another approach" to variables if you need to modify the contents of the variable.Cenozoic
@Cenozoic you can use a constant array with one element - the variable chucklePrimatology
U
4

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

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

Makes no sense whatsoever.

Unreflecting answered 6/4, 2009 at 20:3 Comment(1)
No it's not. Js and python work fine without any special signs. Just admit the truth.Rorie

© 2022 - 2024 — McMap. All rights reserved.