Why is PHP Using Dollar Sign $ to Prefix Variables?

Rasmus Lerdorf - the father of the PHP language, explains the $ sign as an ability to insert variables inside literal string values (interpolation), so the variables are distinguished from the rest of the string. Dollar sign in front of the variable has been inspired by Perl which influenced PHP a lot in the beginning.

Also many other programming languages use the dollar character in their syntax. This symbol is called Sigil and simplifies interpolation among others.

Names not prefixed by $ are considered constants, functions, class names…

Sigil usage simplifies the variable interpolation into strings:


$name = "World";
echo "Hello, $name";

Where as in languages without sigil usage (for example, Python), you must either concatenate strings:

name = "World"
print "Hello, " + name

or use special interpolation syntax if the language provides it. For example, Ruby:

name = "World"
puts "Hello #{name}"

Many people used to other languages might find the sigil usage odd, but you can get used to it in no time and discover the benefits of this.

See Also

GitHub OctocatFound a typo? Something wrong with this content? Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all the contributors.