A variable is a name for some piece of information.
In PHP, variables start with a $ (dollar sign) symbol.
Use the assignment operator (equals sign, =) to assign a value to a variable. Example:
<?php $txt="Hello World!"; print $txt; // outputs "Hello World!" (without quotes) ?>
The variable $txt is assigned the value “Hello World!”.
This type of data — some arbitrary text inside quote marks — is known as a string in computer programming.
Example using numbers:
<?php $a=1; $b=2; print $a + $b; // outputs 3 ?>
Be careful not to confuse the assignment opertor (equals sign, =) with the equality operator (double equals sign, ==).
Rules for naming variables
- A variable name must start with a letter or an underscore “_”
- A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
- A variable name must not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)