PHP Variables - PowerPoint PPT Presentation

About This Presentation
Title:

PHP Variables

Description:

PHP Variables 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. – PowerPoint PPT presentation

Number of Views:265
Avg rating:3.0/5.0
Slides: 27
Provided by: Informati677
Learn more at: http://www1.lasalle.edu
Category:

less

Transcript and Presenter's Notes

Title: PHP Variables


1
PHP Variables
2
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 ones pay rate times the
    number of hours worked
  • Pay PayRate NumberHours

3
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
  • As we said a variable can take on values from a
    set of possible values, the set of values is
    determined in part by the variables type.

4
Types
  • Unlike many other programming languages, PHP
    variables do not have to be declared. In such
    a case, a variables types tends to come from its
    context.
  • Standard types includes numbers (both integers
    and so-called floating point numbers that can
    have fraction parts.
  • Another standard type is a string,which can used
    for names of things or just about any text.

5
PHP variable rules
  • In PHP a variable starts with a dollar sign.
  • After that must come a letter or an underscore
    (no numbers).
  • After that can come letters or numbers.
  • There can be no spaces in the name.

6
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.

7
Turning DateDisplay into a program with a
variable.
8
Result of DateDisplay as variable.
9
Incorporating variables within strings.
  • print "The date is dateString."
  • The above code also shows how variables are used
    in conjunction with strings in PHP.
  • Within the string (set of characters within
    quotes) that will appear literally (as is), the
    variable does not appear as is, rather its value
    is substituted into the string.

10
If you dont want the variable value substituted
in, then use single quotes.
11
Result of single quoted version of DateDisplay.
12
That variable doesnt vary much.
  • Because these are PHP variables, all of their
    variation takes place on the server side (before
    the PHP preprocessed page is sent to the client).
  • PHP variables will ultimately give us interaction
    with the viewer of the page but always through a
    process in which information is sent back to the
    server.

13
The bells, bells, bells example.
  • Before consider variables determined by
    interaction with a user, let us consider one
    example in which the variable does vary.
  • In Edgar Allen Poes poem The Bells, the word
    bells appears over and over again. Lets
    substitute in an image of bells in place of the
    word bells.
  • So far substitution (a lot of it) but not
    variation.

14
The bells, bells, bells example. (Cont.)
  • The first part of the poem talks about silver
    bells, the second about gold bells, and so on.
  • So in this example we will vary the image and the
    font color for each of the different parts.

15
Assigning the variables
Writing some HTML
A chunk of the poem, with the word bells replaced
with the variable bells. Note that the
quotation can be spread out over many lines.
(That would ne a no-no in C.)
Re-assigning the variables
16
Where we left off.
More of the same.
17
Silver bells turning into Golden bells as a
result of our variable being reassigned.
18
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.

19
Pi page (code)
20
Pi page (in browser)
21
PHPs Global Constants
The ltpregt tag tells HTML to respect the white
space of the text between the tags.
The function print_r is for printing arrays
GLOBALS is not one constant but a set of constants
22
(No Transcript)
23
Some of these constants tell one information
about the PHP server in this case the alpha.
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com