Home » Php » syntax – Can PHP be used without the dollar sign $ symbol for variables?

syntax – Can PHP be used without the dollar sign $ symbol for variables?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

define('CONS', 5);
echo CONS;

Answer:

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!

Answer:

Nope. Variables in PHP must start with $.

The other approach is to use constants.

Answer:

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

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

Makes no sense whatsoever.