Lesson 4.3. PHP variables

Syntax

In PHP, variables always start with the character $. This symbol differentiates variables from other entities (functions, keywords ...). A special feature of PHP is that the variables are not typed. It is not necessary to declare the type (int, char, array ...) when creating the variables. The PHP engine will take care of detecting the most suitable type. In the vast majority of cases, this syntax is very user-friendly, even if it (rarely) causes some inconvenience.

Here is an example:

$entier = 5;
$booleen = TRUE;
$chaine = 'String';
$tableau = array(1, 2, 3);

Note here the use of the HTML tag <pre> which allows the browser to display a preformatted text. Line breaks and spaces are displayed as is.

Also note the use of the print_r function which displays the table in a user-friendly form.

Sources of PHP variables

PHP variables are provided by different sources:

Exercice

Creating a PHP script that displays

See also


Last update : 09/17/2022