11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 3 • <strong>PHP</strong> BASICSVariable DeclarationA variable always begins with a dollar sign, $, which is then followed by the variable name. Variablenames follow the same naming rules as identifiers. That is, a variable name can begin with either a letteror an underscore and can consist of letters, underscores, numbers, or other ASCII characters rangingfrom 127 through 255. The following are all valid variables:• $color• $operating_system• $_some_variable• $modelNote that variables are case sensitive. For instance, the following variables bear no relation to oneanother:• $color• $Color• $COLORInterestingly, variables do not have to be explicitly declared in <strong>PHP</strong> as they do in a language such asC. Rather, variables can be declared and assigned values simultaneously. No<strong>net</strong>heless, just because youcan do something doesn’t mean you should. Good programming practice dictates that all variablesshould be declared prior to use, preferably with an accompanying comment.Once you’ve declared your variables, you can begin assigning values to them. Two methodologiesare available for variable assignment: by value and by reference.Value AssignmentAssignment by value simply involves copying the value of the assigned expression to the variableassignee. This is the most common type of assignment. A few examples follow:$color = "red";$number = 12;$age = 12;$sum = 12 + "15"; // $sum = 27Keep in mind that each of these variables possesses a copy of the expression assigned to it. Forexample, $number and $age each possesses their own unique copy of the value 12. If you prefer that twovariables point to the same copy of a value, you need to assign by reference.Reference Assignment<strong>PHP</strong> 4 introduced the ability to assign variables by reference, which essentially means that you cancreate a variable that refers to the same content as another variable does. Therefore, a change to anyvariable referencing a particular item of variable content will be reflected among all other variables57

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

Saved successfully!

Ooh no, something went wrong!