What is $ Sign in PHP: Meaning and Usage Explained
$ sign is used to declare and access variables. It tells PHP that the following name is a variable holding data like numbers or text.How It Works
The $ sign in PHP acts like a label that marks a word as a variable. Think of it as a name tag you put on a box to remember what's inside. Whenever you want to store or use information, you give it a name with $ in front, so PHP knows you are talking about a variable.
For example, if you want to remember someone's age, you write $age. PHP then knows $age holds a value, like 25. This is different from normal words because the $ sign tells PHP to look for the stored value, not just treat it as text.
Example
This example shows how to create a variable with $, assign a value, and print it.
<?php
$greeting = "Hello, friend!";
echo $greeting;
?>When to Use
Use the $ sign whenever you want to store or work with data in PHP. Variables let you keep information like names, numbers, or results from calculations so you can use them later in your program.
For example, if you build a website that asks for a user's name, you store that name in a variable like $username to greet them personally. Or if you calculate a total price, you save it in $total to show or use in other parts of your code.
Key Points
- The
$sign is always placed before variable names in PHP. - Variable names start with a letter or underscore after the
$sign. - Variables store data like text, numbers, or more complex information.
- Using
$helps PHP know you mean a variable, not just a word.