Monday, September 27, 2010

Variables in Php

A variable is a special container that you can define to "hold" a value. A variable
consists of a name that you can choose, preceded by a dollar ($) sign. The variable
name can include letters, numbers, and the underscore character (_). Variable
names cannot include spaces or characters that are not alphanumeric. The following
code defines some legal variables:
$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ
Remember that a semicolon (;) is used to end a PHP statement. The semicolons in
the previous fragment of code are not part of the variable names.
NEW TERM
A variable is a holder for a type of data. It can hold numbers, strings of
characters, objects, arrays, or booleans. The contents of a variable can
be changed at any time . 
As you can see, you have plenty of choices about naming, although it is unusual to
see a variable name that consists exclusively of numbers. To declare a variable, you
need only to include it in your script. You usually declare a variable and assign a
value to it in the same statement.
$num1 = 8;
$num2 = 23;
The preceding lines declare two variables, using the assignment operator (=) to
give them values. You will learn about assignment in more detail in the Operators
and Expressions section later in the hour. After you give your variables values, you
can treat them exactly as if they were the values themselves. In other words
print $num1;
is equivalent to
print 8;
as long as $num1 contains 8.

No comments:

Post a Comment