Variables in PHP begin with a $ symbol, for example:
Variables are assigned values with the = operator, for example:
Note that variable names in PHP are case-sensitive so $myboolean is distinct from $MyBoolean.
Like other scripting languages PHP is weakly typed, that is variables are assigned a type when they are initialised and can change types according to the value assigned. This cannot happened in strongly types languages such as Pascal, Java and C++ where variable types are declared in advance and cannot change.
Operators for numeric variables include:
There are many functions available for using with numbers, for example round() and number_format.
The number_format function inserts commas for thousands and also allows the number of decimal places to be specified:
Numeric variables can be used in calculations such as:
$total=$quantity * $price
Strings are assigned with = and can be output with echo, print and printf. They can be concatenated using the . operator (not + as in most other languages). For example:
Here is some output of variables:
First Name: Billy
Last Name: The Kid
Full Name: BillyThe Kid
Here is the code:
> <?phpNote the concatenation operator '.'
Some more variables on a new page:
Constants are used to provide information that does not change in the course of a program (as distinct from a variable whose contents vary). Constants are defined with the define function, for example:
define ('MYNAME', 'Walter Smurfit');
Now 'MYNAME' can be used anywhere in the PHP code and will always contain 'Walter Smurfit'.
Constant value: Walter Smurfit