Sitepoint : New Articles, Fresh Thinking for Web Developers and Desi...
1 sur 1
PHP Variables, Operators, and Comments By: Kevin Yank June 16th, 2010 Reader Rating: 9
About the Author
Variables in PHP are identical to variables in most other programming languages. For the uninitiated, a variable can be thought of as a name that’s given to an imaginary box into which any literal value may be placed. The following statement creates a variable called $testvariable (all variable names in PHP
begin with a dollar sign) and assigns it a literal value of 3:
Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote Build your own Database Driven Website using PHP and MySQL, a practical step-by-step guide published by SitePoint, and he's co-author of the SitePoint Tech Times, a bi-weekly newsletter for technically-minded web developers. Kev believes that any good webmaster should have seen at least one episode of MacGyver. View all articles by Kevin Y ank...
$testvariable = 3;
PHP is a loosely typed language. This means that a single variable may contain any type of data, be it a number, a string of text, or some other kind of value, and may change types over its lifetime. So the following statement, if you were to type it after the statement above, assigns a new value to the existing $testvariable. In the process, the variable changes type: where it used to contain a number, it now contains a string of text: $testvariable = 'Three';
The equals sign we used in the last two statements is called the assignment operator, as it’s used to assign values to variables. Other operators may be used to perform various mathematical operations on values: $testvariable $testvariable $testvariable $testvariable
= = = =
1 1 2 2
+ * /
1; 1; 2; 2;
// // // //
Assigns Assigns Assigns Assigns
a a a a
value value value value
of of of of
2 0 4 1
From the above examples, you can probably tell that + is the addition operator, - is the subtraction operator, * is the multiplication operator, and / is the division operator. These are all called arithmetic operators, because they perform arithmetic on numbers. Each of the lines above ends with a comment. Comments are a way to describe what your code is doing. They insert explanatory text into your code—text that the PHP interpreter will ignore. Comments begin with // and they finish at the end of the same line. If you need a comment to span several lines, you can instead start your comment with /*, and end it with */. The PHP interpreter will ignore everything between these two delimiters. I’ll use comments throughout the rest of this book to help explain some of the code I present. Returning to the operators, there’s another one that sticks strings of text together, called the string concatenation operator: $testvariable = 'Hi ' . 'there!';
// Assigns a value of 'Hi there!'
Variables may be used almost anywhere that you use a literal value. Consider this series of statements: $var1 = 'PHP'; $var2 = 5; $var3 = $var2 + 1; $var2 = $var1; echo $var1; echo $var2; echo $var3; echo $var1 . ' rules!'; echo "$var1 rules!"; echo '$var1 rules!';
// // // // // // // // // //
Assigns Assigns Assigns Assigns Outputs Outputs Outputs Outputs Outputs Outputs
a value of 'PHP' to $var1 a value of 5 to $var2 a value of 6 to $var3 a value of 'PHP' to $var2 'PHP' 'PHP' '6' 'PHP rules!' 'PHP rules!' '$var1 rules!'
Notice the last two lines in particular. You can include the name of a variable right inside a text string, and have the value inserted in its place if you surround the string with double quotes instead of single quotes. This process of converting variable names to their values is known as variable interpolation. However, as the last line demonstrates, a string surrounded with single quotes will not interpolate the variable names it contains.
Notice the last two lines in particular. You can include the name of a variable right inside a text string, and have the value inserted in i...