11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 3 • <strong>PHP</strong> BASICSwww.it-ebooks.infoVariable VariablesOn occasion, you may want to use a variable whose content can be treated dynamically as a variable initself. Consider this typical variable assignment:$recipe = "spaghetti";Interestingly, you can treat the value spaghetti as a variable by placing a second dollar sign in frontof 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.ConstantsA constant is a value that cannot be modified throughout the execution of a program. Constants areparticularly 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 bechanged (or redefined) at any other point of the program. Constants are defined using the define()function.Defining a ConstantThe define() function defines a constant by assigning a value to a name. Its prototype follows:boolean define(string name, mixed value [, bool case_insensitive])If the optional parameter case_insensitive is included and assigned TRUE, subsequent references tothe constant will be case insensitive. Consider the following example in which the mathematicalconstant 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:66

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!