A variable is a box that stores a value. In PHP, its name always starts with the $ sign, for example $firstname = « Marie »;.
The main types
- String: text between quotes, e.g. « Hello ».
- Integer (int): a whole number, e.g. 42.
- Float: a decimal number, e.g. 3.14.
- Boolean (bool): true or false.
- Array: a list of values.
PHP works out the type on its own from the value you give: there is no need to declare it. This is called dynamic typing.
Displaying a variable
You can concatenate with a dot: echo « Hello » . $firstname;. With double quotes, the variable is even read directly: echo « Hello $firstname »;.
Key point: a variable starts with $, stores a value, and PHP infers its type automatically.