PHPVariables
PHPVariables
PHPVariables
1
What is a variable?
• A variable is something that can be changed – a
thing that can take on any value from a possible
set of values.
– For example, the number of hours someone works in
a given week is a variable.
• In math, a symbol is used in place of the variable
to express a relationship between some quantity
and the variable
– For example, pay is one’s pay rate times the number
of hours worked
– Pay = PayRate * NumberHours
2
What is a variable? (Cont.)
• So a variable is something that stands in for a
quantity that might vary.
• In programming, a variable can be thought of a
referring to a memory location or set of memory
locations. The value of the variable at any given
time is then the contents of that memory location
5
Assignment
• A variable is said to be assigned a value in an
assignment statement. E.g.
$NumberHours = 45;
$WorkerFirstName = “Pete”;
• The assignment statement should not be thought of as
expressing mathematical equality rather it a set of
instructions to evaluate the expression on the right hand
side and give the variable on the left hand side the value
that results. Thus
$Counter = $Counter + 1;
Makes sense as an assignment statement even though
it makes no sense as mathematics.
6
Turning DateDisplay into a program
with a variable.
7
Result of DateDisplay as variable.
8
Incorporating variables within strings.
10
Result of single quoted version of
DateDisplay.
11
That variable doesn’t vary much.
12
The bells, bells, bells example.
13
The bells, bells, bells example. (Cont.)
14
Assigning the variables
16
Silver bells turning
into Golden bells as a
result of our variable
being reassigned.
17
Constants
• If you really want a quantity that is going to
take on a value and never change then
what you want is called a constant.
• Constants do not start with dollar signs.
• By convention, constant names are in all
capital letters.
18
Pi page (code)
19
Pi page (in browser)
20
PHP’s Global Constants
21
22
Some of these
constants tell
one information
about the PHP
server – in this
case the alpha.
23
24
25
26