Using Variable and Operators
variable
A variable is simply a container thats used to store both
numeric and non-numeric information.
PHP has some simple rules for naming variables.
Every variable name must be preceded with a dollar ($) symbol
and must begin with a letter or underscore character, optionally
followed by more letters, numbers, or underscore characters.
Common punctuation characters, such as commas, quotation
marks, or periods, are not permitted in variable names; neither
are spaces.
examples
for example, $root, $_num, and $query2 are
all valid variable names, while $58%, $1day,
and email are all invalid variable names.
Assigning Values to Variables
Assigning a value to a variable in PHP is quite easy:
use the equality (=) symbol, which also happens to
be PHPs assignment operator. This assigns the
value on the right side of the equation to the
variable on the left.
<?php
// assign value to variable
$name = 'Simon';
?>
You can also assign a variable the value of
another variable, or the result of a calculation.
<?php
$now = 2008;
$currentYear = $now;
// perform calculation
$lastYear = $currentYear - 1;
echo "$lastYear has ended. Welcome to
$currentYear!";
?>
// output: '2007 has ended. Welcome to 2008!'
Destroying Variables
To destroy a variable, pass the variable to PHPs aptly
named unset() function, as in the following example:
<?php
// assign value to variable
$car = 'Porsche';
// output: 'Before unset(), my car is a Porsche'
echo "Before unset(), my car is a $car";
// destroy variable
unset($car);
// this will generate an 'undefined variable' error
// output: 'After unset(), my car is a '
echo "After unset(), my car is a $car";
?>
Inspecting Variable Contents
PHP offers the var_dump() function, which
accepts a variable and X-rays it for you.
Heres an example:
<?php
// define variables
$name = 'Fiona';
$age = 28;
// display variable contents
var_dump($name);
var_dump($age);
?>
THANK YOU