Variables
All variables in PHP start with a dollar sign ($). You can assign values to variables using the assignment operator (=). In the example below we make a variable with a string and a variable with a number.
One of the features of PHP is that a variable does not need to be declared before using it. In this case a variable will be created when you first assign a value to it, so that a variable type refers to the kind of data that is stored in it.
If the variable hold a string, there are required quotation marks.
<?php
$variable = "Hello PHP";
$another_variable = 5;
echo $variable;
echo $another_variable;
?>
There are a few variable naming rules that you need to observe when choosing a name for your PHP variables:
- PHP variables must start with a letter or underscore (_),
- Variables can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
- A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_Variable). Also variables with more than one word can be discerned with capitalization ($myVariable).