PHP Lessons

Simple variables

webbino_guardaphp (8K)

Generally a PHP variable has no need to be declared, as happens in other programming languages (like pascal).

The name of a variable must be preceded by the $ symbol, it can include letters, digits and the "_" (underscore) character, but it cannot begin with a digit.

Examples of names

$a $pippo $a12 $count $ok $mario_rossi

Assignment

A simple variable is like a box: the name of the variable, for example $var, is its identifying label.

The PHP assignment instruction puts a value inside the box:

$var = 8;$var is the label, the number 8 is the box content
The = symbol doesn't mean equality, but assignment!

A simple variable can hold only one value at a time: each new assignment deletes the previous one.

Types of variables

The variable types depend on their content. The simple variable types can be:

Some example

$a = 13;assignment of a numerical value to $a
$a = "giovanni";assignment of a string to $a
$a = TRUE;assignment of the TRUE boolean constant to $a
$a = $a + 1;increase of 1 the numerical value of $a
$a++;equivalent instruction of $a = $a + 1;
$a = $a + $c;assigns the sum of the numerical values $b and $c to $a

Valid HTML 4.01 Transitional