Prev | Current Page 161 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Consider the following example in
which the mathematical constant PI is defined:
define("PI", 3.141592);
The constant is subsequently used in the following listing:
printf("The value of pi is %f", PI);
$pi2 = 2 * PI;
printf("Pi doubled equals %f", $pi2);
This code produces the following results:
The value of pi is 3.141592.
Pi doubled equals 6.283184.
There are several points to note regarding the previous listing. The first is that constant
references are not prefaced with a dollar sign. The second is that you can??™t redefine or
undefine the constant once it has been defined (e.g., 2*PI); if you need to produce a
value based on the constant, the value must be stored in another variable. Finally,
constants are global; they can be referenced anywhere in your script.
Expressions
An expression is a phrase representing a particular action in a program. All expressions
consist of at least one operand and one or more operators. A few examples follow:
$a = 5; // assign integer value 5 to the variable $a
$a = "5"; // assign string value "5" to the variable $a
$sum = 50 + $some_int; // assign sum of 50 + $some_int to $sum
$wine = "Zinfandel"; // assign "Zinfandel" to the variable $wine
$inventory++; // increment the variable $inventory by 1
Operands
Operands are the inputs of an expression. You might already be familiar with the manipulation
and use of operands not only through everyday mathematical calculations, but
also through prior programming experience.


Pages:
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173