A variable is a place where you can store things, such as number, a date or some text. You can put the text or number into variables so you can retrive them later or so you can manipulate them. Variables are called variables beacuse the value that they can vary.
The stored value can be referenced using the variable’s name and changed as the script proceeds. The script author can choose any name for a variable providing it adheres to these three naming conventions:
- Name must begin with a $ dollar sign, for example, $name
- Name can comprise letters, numbers, and underscore characters, but not spaces – for example, $subtotal_1
- The first character after the $ dollar sign must be a letter or an underscore character – it cannot be a number.
Note that variable names in PHP are case-sensitive so $name, $Name, and $NAME are three separate individual variables.
PHP variables are loosely typed meaning they can contain data of any type, unlike “strongly typed” variables in some languages where the data type must be specified when the variables are created. So, PHP variables may happily contain an integer, or a floating-point number, or a string of text characters, or a Boolean value of True or FALSE, or an object, or a NULL empty value.
Do not confuse the purpose of double and single quotes. Remember that PHP only makes variables substitutions for mixed strings enclosed with double quotes.
A variable is created in PHP script simply by stating its name. The variable can then be assigned an initial value (initialized) by using the = assignment operator to state its value. This statement, and all others in PHP, must end with a semi-colon like this:
The value contained within the variable can then be displayed by referencing it using the variable like this :