One of the most common things you'll be doing in PHP, apart from using '<?php' tags, is using the dollar sign ($), countlessly. But what does the dollar sign do, why are all variables in PHP prefixed with it, and what can and can't be used with them? All those questions are answered with code examples to show you how. Let's jump in and find out why PHP uses dollar signs in its codebase.
What do the dollar signs do in PHP?
The dollar sign in PHP is a reserved symbol that's used by the parser to determine what you've defined as a compile-time variable. In PHP, there are certain words and symbols that have been reserved to allow the parser to process the script of code and to determine code blocks by setting clear rules. These rules (in other words reserved names) are a way to avoid naming conflicts with core PHP functions, variables, and classes (amongst others) with yours. PHP uses the dollar symbol in its syntax to distinguish it from the rest of the string (script). Taking inspiration from Perl, which PHP had a lot of similarities to back in the early days of the programming language, used symbols (Sigil's) to show variables and their scope. The below example shows we have a variable called foo, where the dollar sign ($) is the sigil and foo is the variable name, which has a scalar data type (bool, int, float, double, char, or string), in this case, a string with the value test.
# Dollar sign is used to tell PHP that 'foo' is a variable
$foo = 'test';
Other programming languages don't use sigils, like dollar signs in PHP, to determine variables, and others like Ruby use interpolation syntax, which allows you to reference variables in a similar way PHP does with the dollar symbol.
Examples of PHP keywords not requiring a dollar sign
Class constants (const) are one area that might appear to require a dollar sign, but doesn't (and shouldn't). They are generally called names (or labels) and therefore not a variable and therefore don't require a dollar sign.
# The const reserved word does not start with a $
const VAT_RATE = 20;
Using Interpolation Syntax in PHP
It's possible in PHP to use interpolation syntax, that is, a variable inside a string, which gets interpreted. The string must contain quotes, not single quotes in order to work correctly. Echoing with single quotes and whilst using a variable will output the variable as is. Meaning, instead of PHP echoing out the contents of the variable, it will instead output the extact provided statement. In the case of the second example, it will echo $foo. To avoid this, always use double quotes when you put a variable inside a string context. In the third example below, we have given an exmaple of an echo statement along with a variable to show it is still possible to use single quotes, just the variable is instead concatenated to the string. Concatenation is a form connecting things together in a programming language. In PHP we do this using the period symbol "." which we've added between the string and the variable. PHP's engine will concatenate the string and the variable's content as the fully prepared echo statement.
# Dollar sign is used to tell PHP that 'foo' is a variable
echo "$foo";
# Single quotes are incorrect
echo '$foo';
# Concatenation with single quotes
echo 'The time is: ' . $foo;
Do dollar, dollar signs work in PHP?
Yes, dollar, dollar ($$) signs do also work in PHP. The idea behind double dollar signs in PHP is to allow developers to dynamically name variables at compile time. If you see double dollars in open-source software, or in another code repository, it's valid syntax, also called variable variables.
Conclusion
The dollar sign is used in PHP to determine within a given string mainly a PHP script, what are variables as opposed to traits, classes, functions, const, and other PHP core keywords. In PHP this sign makes up a huge part of the how the language works and how you can start to create and read other people's PHP code. The dollar sign use throughout PHP scripts and ultimately web applications is one that you will use a lot as a PHP developer. That is because PHP knows that a dollar sign followed by a string name is a variable and that variable will be treated differently to other parts of the language such as an array.
- Add a dollar sign ($) to all variables, prefixed before the variable name
- Don't use dollar signs elsewhere otherwise, you'll get syntax errors
- Double dollars can also be used and are valid PHP syntax
- When combining a string and a variable concatenate them together, or use double quotes