Variables in PHP

phpVariables are basically used to store some value.  Variables can be local or global.  Local variables are declared and accessible only inside a function.  Global variables are always declared outside a function and are accessible inside any function as well as outside.  Global variables are basically declared at the very beginning of a code file.  This will make things easy for us if we want to make some changes in global variables instead of declaring them randomly somewhere and then wasting our time in finding them for modification.

In PHP, a variable always starts with a dollar ($) sign.  They can contain numbers, letters, dashes, and underscores but basically developers avoid using dashes.  In case of underscores, you should only use single underscore to separate 2 letters.  However, you can use more than that but it is good practice to use single underscore only because sometime its hard to identify on a first glance how many underscore you have used in a variable to separate letters and it can mess up your entire project if not followed properly throughout your entire code.  There should not be any space between letters.  For example:

$My Variable => Wrong
$MyVariable => Correct
$My_Variable => Correct
$My-Variable => Correct

Variables in PHP are case-sensitive. So, you must make sure you use them as you declared them.  There are some predefined keywords in PHP, which you should avoid using as variable name.  Click Here for the list.

$hello = "Hello World!";
echo $hello;