The PHP Scripting Language - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

The PHP Scripting Language

Description:

There are other places to put PHP as we will see later. Each block of code ... meta http-equiv='Content-Type content='text/html; charset=iso-8859-1' Types ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 19
Provided by: paws3
Category:

less

Transcript and Presenter's Notes

Title: The PHP Scripting Language


1
Chapter 2
  • The PHP Scripting Language

2
Topics
  • PHP Basics
  • Script structure
  • Variables
  • Supported Types
  • Constants
  • Expressions
  • Type Conversions
  • Condition and branch statements
  • If, ifelse, switch
  • Looping structures
  • User-defined functions

3
Hello
Each block of code ends with a semi-colon.
Block of PHP code. Note that the code starts and
ends with a tag lt? ?gt and is embedded inside
the HTML. There are other places to put PHP as
we will see later.
4
PHP Basics
  • Three variations for tags
  • lt?php ?gt
  • This is the version we will use in the class
  • lt? ?gt
  • ltscript languagePHPgt lt/scriptgt
  • Whitespace has no effect except to aid
    readability for the developer
  • lt?php print Hello ?gt
  • A script is a series of statements. Each
    statement is teminated with a semi-colon
  • When the script is run, the entire script is
    replaced with the output of the code.

5
Variables
  • Identified by the dollar sign followed by the
    variable name.
  • Unlike many other languages, you do not need to
    declare the variable before assigning it a value
  • var 15
  • They are case sensitive
  • Variables are placeholders for data
  • lt?php outputString Hello, world ?gt
  • lt?php print outputString ?gt

6
Comments
  • // This is a one-line comment
  • This is another one-line comment style
  • / Use this for
  • multi-line comments
  • /

7
Echo and Print
  • Can be use the same way
  • print Hello, world.
  • echo Hello, world.
  • The difference between the two is that echo can
    output more than one parameter separated by a
    comma
  • // prints The answer is 42
  • echo The answer is, 42

8
String Literals
  • String literals are the same as strings in PHP
  • print This works the same as
  • print this.
  • Escaping
  • The escape character is the backslash \

9
Variable Substitution
  • You can place a variable directly in a string but
    it is best to use curly braces for
    readability.
  • number 45
  • vehicle bus
  • message This vehicle holds number
    people
  • // This way is a little more difficult to read
  • message2 This vehicle holds number people

10
Character Encoding
  • To correctly interpret characters, PHP needs to
    know the character encoding of the file. More
    simply, PHP needs to know what each 8-bit
    sequence that makes up the character means.
  • PHP reads ISO-8859-1
  • Equivalent to 7-bit ASCII for the first 127
    characters
  • Instruct your browser to use this by adding the
    following line to your HTML ltheadgtlt/headgt
    section
  • ltmeta http-equivContent-Type contenttext/html
    charsetiso-8859-1gt

11
Types
  • PHP has four scalar types
  • Boolean, float, integer, and string
  • Two compound types
  • Array and object
  • And NULL
  • Used when a variable does not have a value

12
Types
  • Boolean are assigned either true or false
  • var false
  • test
  • Integers are the same as counting numbers
  • myInt 6
  • Floats have a decimal place and can also be
    represented by exponential notation
  • myFloat 6.6
  • myFloat2 1.12e3 // equals 1120
  • myFloat3 2e-2 // equals 0.02

13
Constants
  • Constants associate a name with a scalar value
  • Unlike variables, they are not preceded by a
    character
  • By convention constants are all uppercase
  • define (NORMALTEMPERATURE, 96.8)
  • print NORMALTEMPERATURE
  • PHPs predefined constants use the first letter
    of the library name
  • // This outputs 3.14159265358
  • // and uses the letter M to represent the math
    library
  • print M_PI

14
Expressions, Operators, and Variable Assignment
  • The value on the right side of the equals sign is
    called the expression
  • // Simple example
  • var 4 7
  • // These all add one to var
  • var var 1
  • var 1
  • var // This is the most common way to
    increment in PHP
  • // These all subtract one from var
  • var var 1
  • var - 1
  • var-- // again this notation is the most common
  • //Double a value
  • var var 2
  • var 2

15
String Expressions
  • To concatenate a string you use the dot operator
  • // Assign a string value to a variable
  • strSnow snow day.
  • var I want a . strSnow

16
Operator Precedence
  • PHP needs to decide which operator takes
    precedence.
  • Here is the order
  • Multiplication occur before subtraction and
    addition
  • To avoid confusion, use parentheses to make you
    code clear.
  • var 2 5 6
  • // better to do it this way for clarity
  • var 2 (5 6)

17
Conditions and Branches If Else
  • Without the braces the if statement only executes
    one line.
  • if (grade gt 90)
  • print A
  • elseif (grade gt 80)
  • print B
  • else
  • Your grade is a C or lower
  • Multiple statements need the curly braces
  • if (var gt 100)
  • print The variable is greater than 100
  • // We now need to reset the variable back to
    zero
  • var 0

18
Conditions and Branches switch Statements
  • The switch statement can be used as an
    alternative to the if statement.
  • switch(country)
  • case USA
  • echo ltpgtAmericanlt/pgt
  • break
  • case Belgium
  • echo ltpgtFlemmishlt/pgt
  • break
Write a Comment
User Comments (0)
About PowerShow.com