Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dollar notation in script languages - why? [closed]

Does anyone know, what is the actual reason behind "dollar variable notation" in some of the scripting languages, i.e. PHP or Perl? Python creators did not find $variable useful, neither do I. Why does PHP and Perl force me to press shift-4 so often?

(OK, in Perl you can somehow explain it with distinguishing $scalar, @array and %hash but still it could be successfully avoided there, type does not need to be explicit)

like image 920
Jakub M. Avatar asked Aug 03 '11 05:08

Jakub M.


People also ask

Why do we use a dollar symbol ($) before variables 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.

What does the 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.

Why does PHP start with variables?

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 is the use of symbol in PHP explain it with an example?

The @ symbol is the error control operator ("silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.


2 Answers

I don't know if this is the main reason, but if PHP did not have variables with an identifying marker, the following would not be possible.

echo "Welcome back $name, we last saw you on $date";

Update:

It also creates something called a variable variable possible. The usefulness of a variable variable is debatable though, and I wouldn't recommend anyone making regular use of them.

like image 109
AndrewR Avatar answered Sep 22 '22 23:09

AndrewR


One of the design goals with Perl's variables was to make them visually distinguishable from builtins, reserved words, control flow words, and so on. To someone unfamiliar with Perl, all the clues may look like line noise. But to someone proficient with Perl, the "sigils", as they're called, make code more readable and more understandable. As you already observed, the type of sigil in Perl5 changes depending on the underlying data being accessed. $ for scalar, @ for array, and % for hash.

But there is the additional visual cue that $hash{key} is referring to the scalar value held in the hash. The $ sigil indicates scalar, and the {} brackets indicate that we're indexing into a hash (just as an example). In the case of indexing into an array, $array[0], for example: The $ tells us we're accessing the scalar value held in the array, made obvious by the square brackets, at the index point within the brackets. When we see @array[1, 2, 3], we have an immediate visual notice by way of the @ that we're grabbing a list of elements (assuming a list context) from an array (the square brackets still tell us we're getting them from an array). That's an array slice. A hash slice would be @hash{qw/ this that the other/}. So once again that @ tells us to be looking for a list, and the curly brackets tell us we're getting them from a hash. When we see a @ or a % without postfix brackets, we're taking the entire array or hash.

The cues become second nature quickly.

Perl6 is a little different, and will take proficient Perl5 users a few days to get used to. ;)

There's a good Wikipedia article on the subject here: Sigil (Computer Programming)

Aside from the visual guide, the sigils make other things easier too; dereferencing and interpolation, for example. While more elaborate operators could have been used for something like interpolation (and are still necessary occasionally to disambiguate where a variable ends and adjoining word characters continue), the goal was probably to keep simple things easy, while making hard things possible.

like image 41
DavidO Avatar answered Sep 25 '22 23:09

DavidO