Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 879
Ali Avatar asked Apr 06 '09 19:04

Ali


People also ask

Why do we use a dollar symbol ($) before variables in PHP?

Rasmus Lerdorf, the father of the PHP language, explains the $ sign as an ability to insert variables inside literal string values (interpolation), so that the variables are distinguished from the rest of the string.

What does a $$$ mean in PHP?

The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value. These are called variable variables in PHP.

Which symbol do PHP variables start with?

Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number.

What does a dollar sign mean in PHP?

$ is the way to refer to variables in PHP. Variables in PHP are dynamically typed, which means that their type is determined by what's assigned to them. Here's the page about variables from the PHP manual. $a = "This is a string"; $b = 1; // This is an int.


2 Answers

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

define('CONS', 5);
echo CONS;
like image 188
Peter Avatar answered Oct 08 '22 01:10

Peter


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!

like image 35
Dan Rosenstark Avatar answered Oct 08 '22 01:10

Dan Rosenstark