views:

579

answers:

5

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?

+46  A: 

No.

chaos
Couldn't have said it better myself.
Chacha102
Sorry, but 36 upvotes? Really?
Daniel
Don't hate the player, hate the player character.
chaos
+3  A: 

Nope. Variables in PHP must start with $.

The other approach is to use constants.

uuɐɯǝʃǝs
Constants aren't "another approach" to variables if you need to modify the contents of the variable.
Rob
+6  A: 

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

define('CONS', 5);
echo CONS;
Peter
He clearly said "variables", which isn't "close" to constants in any real sense at all.
Rob
It is quite the opposite in fact.
akway
+4  A: 

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

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

Makes no sense whatsoever.

karim79
+2  A: 

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!

Yar
Good point. I think you still need to use + signs in Java, someone correct me if I'm wrong :)
Click Upvote
Groovy has variable interpolation. ;)
Rob
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...
Yar
@Rob: thanks for that, and for telling me it's called "variable interpolation." Also see this http://www.koders.com/java/fid39C2E8453EEC56A6A2A9DFC5FFE7F6E4F7066527.aspx
Yar
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
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 :)
Yar