Prev | Current Page 160 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Variable Variables
On occasion, you may want to use a variable whose content can be treated dynamically
as a variable in itself. Consider this typical variable assignment:
$recipe = "spaghetti";
Interestingly, you can treat the value spaghetti as a variable by placing a second
dollar sign in front of the original variable name and again assigning another value:
$$recipe = "& meatballs";
This in effect assigns & meatballs to a variable named spaghetti.
Therefore, the following two snippets of code produce the same result:
echo $recipe $spaghetti;
echo $recipe ${$recipe};
The result of both is the string spaghetti & meatballs.
Constants
A constant is a value that cannot be modified throughout the execution of a program.
Constants are particularly useful when working with values that definitely will not
require modification, such as pi (3.141592) or the number of feet in a mile (5,280).
Once a constant has been defined, it cannot be changed (or redefined) at any other
point of the program. Constants are defined using the define() function.
Defining a Constant
The define() function defines a constant by assigning a value to a name. Its prototype
follows:
boolean define(string name, mixed value [, bool case_insensitive])
CHAPTER 3 ?–  PHP BASICS 87
If the optional parameter case_insensitive is included and assigned TRUE, subsequent
references to the constant will be case insensitive.


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