Why does PHP use a 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 that the variables are distinguished from the rest of the string. A dollar sign in front of variables in PHP is inspired by Perl which greatly influenced PHP during its early years.

Many other programming languages also use a dollar sign in their syntax. This symbol is called "sigil" and simplifies interpolation.

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

Sigil usage simplifies the variable interpolation into strings:

<?php

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

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

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

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

# 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 its benefits.

See also



Found 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 contributors.