Among the most important concepts in programming is the notion of variable. It is a notion that must be assimilated and mastered in order to be able to undertake the first basic applications. We will see that variables are part of the mechanisms that make an application dynamic. Let’s get to the heart of the matter.
What is a variable?
A variable is a data structure of primitive type (integer, real, character, string, Boolean or null) or of structured type (array or object) that allows to store one or more values. Each value of a variable can be replaced by another one during the execution of the program. Hence the term “variable”. In programming, a variable is defined according to 4 essential pieces of information listed below:
- A name
- A type
- One / more values
A semantic (a meaning) after the operations performed on this variable. More concretely, is the value of the variable logical in relation to its initial context?
Schematically, a variable can be assimilated to an opaque box on which a label with a name (the name of the variable) is stuck. Inside this package is something (the value) of a precise type (the type of the variable). When we want to get this value, we don’t address it directly but we refer to it by selecting the box by its name.
Examples variable in PHP
<?php $firstname= 'Mario'; $lastname= "Peach"; $age = 22; ?>