Low cost ecommerce web development India flash website design
Variables and Operators
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
$testvariable (all variable names in PHP begin with a dollar sign) and assigns
it a value of 3:
$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 it appears
after the statement above, assigns a new value to our 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";
52
Getting Started with PHP
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:
$testvariable = 1 + 1; // Assigns a value of 2
$testvariable = 1 - 1; // Assigns a value of 0
$testvariable = 2 * 2; // Assigns a value of 4
$testvariable = 2 / 2; // Assigns a value of 1
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 // and they finish at the end of
the same line. You might be familiar with /* */ style comments in other languages
—these work in PHP as well. I'll be using comments throughout the rest
of this book to help explain what the code I present is doing.
Now, to get back to the four statements above, the operators we used are called
the arithmetic operators, and 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:
$testvariable = "Hi " . "there!";
// Assigns a value of "Hi there!"
Variables may be used almost anywhere that you use an actual value. Consider
these examples:
$var1 = 'PHP'; // Assigns a value of "PHP" to $var1
$var2 = 5; // Assigns a value of 5 to $var2
$var3 = $var2 + 1; // Assigns a value of 6 to $var3
$var2 = $var1; // Assigns a value of "PHP" to $var2
echo($var1); // Outputs "PHP"
echo($var2); // Outputs "PHP"
echo($var3); // Outputs 6
echo($var1 . ' rules!'); // Outputs "PHP rules!"
echo("$var1 rules!"); // Outputs "PHP rules!"
echo('$var1 rules!'); // Outputs '$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. 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.
53
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110