Variables and Operators - Page 2January 17, 2002 Variables in PHP are identical to variables in most other
programming languages. For the uninitiated, a variable is a name
given to an imaginary box into which any value may be placed. The
following statement creates a variable called
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 it appears after the
statement above, assigns a new value to our existing
The equals sign we used in the last two statements is called the assignment operator, as it is used to assign values to variables. Other operators may be used to perform various mathematical operations on values:
The lines above each end with a comment. Comments are a way to
describe what your code is doing - they insert explanatory text
into your code, and tell the PHP interpreter to ignore it.
Comments begin with Now, to get back to the four statements above, the operators we used here allow you to add, subtract, multiply, and divide numbers. Among others, there is also an operator that sticks strings of text together, called the concatenation operator:
Variables may be used almost anywhere that you use an actual value. Consider these examples:
Notice the last two lines especially. 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. This process of converting variable names to their values is known in technical circles as variable interpolation. However, as the last line demonstrates, a string surrounded with single quotes will not interpolate variable names within the string. |